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_ENGINE_PERFETTO_SQL_PREPROCESSOR_H_ 18 #define SRC_TRACE_PROCESSOR_PERFETTO_SQL_ENGINE_PERFETTO_SQL_PREPROCESSOR_H_ 19 20 #include <optional> 21 #include <string> 22 #include <unordered_map> 23 #include <unordered_set> 24 #include <vector> 25 26 #include "perfetto/base/status.h" 27 #include "perfetto/ext/base/flat_hash_map.h" 28 #include "perfetto/ext/base/status_or.h" 29 #include "src/trace_processor/sqlite/sql_source.h" 30 #include "src/trace_processor/sqlite/sqlite_tokenizer.h" 31 32 namespace perfetto::trace_processor { 33 34 // Preprocessor for PerfettoSQL statements. The main responsiblity of this 35 // class is to perform similar functions to the C/C++ preprocessor (e.g. 36 // expanding macros). It is also responsible for splitting the given SQL into 37 // statements. 38 class PerfettoSqlPreprocessor { 39 public: 40 struct Macro { 41 bool replace; 42 std::string name; 43 std::vector<std::string> args; 44 SqlSource sql; 45 }; 46 47 // Creates a preprocessor acting on the given SqlSource. 48 explicit PerfettoSqlPreprocessor( 49 SqlSource, 50 const base::FlatHashMap<std::string, Macro>&); 51 52 // Preprocesses the next SQL statement. Returns true if a statement was 53 // successfully preprocessed and false if EOF was reached or the statement was 54 // not preprocessed correctly. 55 // 56 // Note: if this function returns false, callers *must* call |status()|: it 57 // is undefined behaviour to not do so. 58 bool NextStatement(); 59 60 // Returns the error status for the parser. This will be |base::OkStatus()| 61 // until an unrecoverable error is encountered. status()62 const base::Status& status() const { return status_; } 63 64 // Returns the most-recent preprocessed SQL statement. 65 // 66 // Note: this function must not be called unless |NextStatement()| returned 67 // true. statement()68 SqlSource& statement() { return *statement_; } 69 70 private: 71 struct MacroInvocation { 72 const Macro* macro; 73 std::unordered_map<std::string, SqlSource> arg_bindings; 74 }; 75 76 base::StatusOr<SqlSource> RewriteInternal( 77 const SqlSource&, 78 const std::unordered_map<std::string, SqlSource>& arg_bindings); 79 80 base::StatusOr<MacroInvocation> ParseMacroInvocation( 81 SqliteTokenizer& tokenizer, 82 SqliteTokenizer::Token& token, 83 const SqliteTokenizer::Token& name_token, 84 const std::unordered_map<std::string, SqlSource>& arg_bindings); 85 86 SqliteTokenizer global_tokenizer_; 87 const base::FlatHashMap<std::string, Macro>* macros_ = nullptr; 88 std::unordered_set<std::string> seen_macros_; 89 std::optional<SqlSource> statement_; 90 base::Status status_; 91 }; 92 93 } // namespace perfetto::trace_processor 94 95 #endif // SRC_TRACE_PROCESSOR_PERFETTO_SQL_ENGINE_PERFETTO_SQL_PREPROCESSOR_H_ 96