1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_PREPROCESSOR_PERFETTO_SQL_PREPROCESSOR_H_
18 #define SRC_TRACE_PROCESSOR_PERFETTO_SQL_PREPROCESSOR_PERFETTO_SQL_PREPROCESSOR_H_
19 
20 #include <optional>
21 #include <string>
22 #include <unordered_set>
23 #include <vector>
24 
25 #include "perfetto/base/status.h"
26 #include "perfetto/ext/base/flat_hash_map.h"
27 #include "src/trace_processor/perfetto_sql/tokenizer/sqlite_tokenizer.h"
28 #include "src/trace_processor/sqlite/sql_source.h"
29 
30 namespace perfetto::trace_processor {
31 
32 // Preprocessor for PerfettoSQL statements. The main responsiblity of this
33 // class is to perform similar functions to the C/C++ preprocessor (e.g.
34 // expanding macros). It is also responsible for splitting the given SQL into
35 // statements.
36 class PerfettoSqlPreprocessor {
37  public:
38   struct Macro {
39     bool replace;
40     std::string name;
41     std::vector<std::string> args;
42     SqlSource sql;
43   };
44 
45   // Creates a preprocessor acting on the given SqlSource.
46   explicit PerfettoSqlPreprocessor(
47       SqlSource,
48       const base::FlatHashMap<std::string, Macro>&);
49 
50   // Preprocesses the next SQL statement. Returns true if a statement was
51   // successfully preprocessed and false if EOF was reached or the statement was
52   // not preprocessed correctly.
53   //
54   // Note: if this function returns false, callers *must* call |status()|: it
55   // is undefined behaviour to not do so.
56   bool NextStatement();
57 
58   // Returns the error status for the parser. This will be |base::OkStatus()|
59   // until an unrecoverable error is encountered.
status()60   const base::Status& status() const { return status_; }
61 
62   // Returns the most-recent preprocessed SQL statement.
63   //
64   // Note: this function must not be called unless |NextStatement()| returned
65   // true.
statement()66   SqlSource& statement() { return *statement_; }
67 
68  private:
69   SqliteTokenizer global_tokenizer_;
70   const base::FlatHashMap<std::string, Macro>* macros_ = nullptr;
71   std::unordered_set<std::string> seen_macros_;
72   std::optional<SqlSource> statement_;
73   base::Status status_;
74 };
75 
76 }  // namespace perfetto::trace_processor
77 
78 #endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_PREPROCESSOR_PERFETTO_SQL_PREPROCESSOR_H_
79