xref: /aosp_15_r20/external/perfetto/src/trace_processor/perfetto_sql/tokenizer/sqlite_tokenizer.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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 #include "src/trace_processor/perfetto_sql/tokenizer/sqlite_tokenizer.h"
18 
19 #include <cstdint>
20 #include <string>
21 #include <string_view>
22 #include <utility>
23 
24 #include "perfetto/base/logging.h"
25 #include "src/trace_processor/sqlite/sql_source.h"
26 
27 namespace perfetto::trace_processor {
28 extern "C" {
29 
30 int sqlite3GetToken(const unsigned char* z, int* tokenType);
31 }
32 
SqliteTokenizer(SqlSource sql)33 SqliteTokenizer::SqliteTokenizer(SqlSource sql) : source_(std::move(sql)) {}
34 
Next()35 SqliteTokenizer::Token SqliteTokenizer::Next() {
36   Token token;
37   const char* start = source_.sql().data() + offset_;
38   int n = sqlite3GetToken(reinterpret_cast<const unsigned char*>(start),
39                           &token.token_type);
40   offset_ += static_cast<uint32_t>(n);
41   token.str = std::string_view(start, static_cast<uint32_t>(n));
42   return token;
43 }
44 
NextNonWhitespace()45 SqliteTokenizer::Token SqliteTokenizer::NextNonWhitespace() {
46   Token t;
47   for (t = Next(); t.token_type == TK_SPACE; t = Next()) {
48   }
49   return t;
50 }
51 
NextTerminal()52 SqliteTokenizer::Token SqliteTokenizer::NextTerminal() {
53   Token tok = Next();
54   while (!tok.IsTerminal()) {
55     tok = Next();
56   }
57   return tok;
58 }
59 
Substr(const Token & start,const Token & end,EndToken end_token) const60 SqlSource SqliteTokenizer::Substr(const Token& start,
61                                   const Token& end,
62                                   EndToken end_token) const {
63   auto offset = static_cast<uint32_t>(start.str.data() - source_.sql().c_str());
64   const char* e =
65       end.str.data() +
66       (end_token == SqliteTokenizer::EndToken::kInclusive ? end.str.size() : 0);
67   auto len = static_cast<uint32_t>(e - start.str.data());
68   return source_.Substr(offset, len);
69 }
70 
SubstrToken(const Token & token) const71 SqlSource SqliteTokenizer::SubstrToken(const Token& token) const {
72   auto offset = static_cast<uint32_t>(token.str.data() - source_.sql().c_str());
73   auto len = static_cast<uint32_t>(token.str.size());
74   return source_.Substr(offset, len);
75 }
76 
AsTraceback(const Token & token) const77 std::string SqliteTokenizer::AsTraceback(const Token& token) const {
78   PERFETTO_CHECK(source_.sql().c_str() <= token.str.data());
79   PERFETTO_CHECK(token.str.data() <=
80                  source_.sql().c_str() + source_.sql().size());
81   auto offset = static_cast<uint32_t>(token.str.data() - source_.sql().c_str());
82   return source_.AsTraceback(offset);
83 }
84 
Rewrite(SqlSource::Rewriter & rewriter,const Token & start,const Token & end,SqlSource rewrite,EndToken end_token) const85 void SqliteTokenizer::Rewrite(SqlSource::Rewriter& rewriter,
86                               const Token& start,
87                               const Token& end,
88                               SqlSource rewrite,
89                               EndToken end_token) const {
90   auto s_off = static_cast<uint32_t>(start.str.data() - source_.sql().c_str());
91   auto e_off = static_cast<uint32_t>(end.str.data() - source_.sql().c_str());
92   uint32_t e_diff = end_token == EndToken::kInclusive
93                         ? static_cast<uint32_t>(end.str.size())
94                         : 0;
95   rewriter.Rewrite(s_off, e_off + e_diff, std::move(rewrite));
96 }
97 
RewriteToken(SqlSource::Rewriter & rewriter,const Token & token,SqlSource rewrite) const98 void SqliteTokenizer::RewriteToken(SqlSource::Rewriter& rewriter,
99                                    const Token& token,
100                                    SqlSource rewrite) const {
101   auto s_off = static_cast<uint32_t>(token.str.data() - source_.sql().c_str());
102   auto e_off = static_cast<uint32_t>(token.str.data() + token.str.size() -
103                                      source_.sql().c_str());
104   rewriter.Rewrite(s_off, e_off, std::move(rewrite));
105 }
106 
107 }  // namespace perfetto::trace_processor
108