• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2017 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef AST_UTIL_H_
16 #define AST_UTIL_H_
17 
18 #include <clang/AST/AST.h>
19 
20 #include <map>
21 #include <string>
22 
23 
24 namespace header_checker {
25 namespace dumper {
26 
27 
28 constexpr static char type_id_prefix[] = "type-";
29 
30 struct ASTCaches {
ASTCachesASTCaches31   ASTCaches(const std::string &translation_unit_source)
32       : translation_unit_source_(translation_unit_source) {}
33 
GetTypeIdASTCaches34   std::string GetTypeId(const std::string &qual_type) {
35     auto type_id_it = qual_type_to_type_id_cache_.find(qual_type);
36     if (type_id_it == qual_type_to_type_id_cache_.end()) {
37       qual_type_to_type_id_cache_.insert(
38           std::make_pair(qual_type, ++max_type_id_));
39           return type_id_prefix + std::to_string(max_type_id_);
40     }
41     return type_id_prefix + std::to_string(type_id_it->second);
42   }
43 
44   std::string translation_unit_source_;
45   std::set<std::string> type_cache_;
46   std::map<const clang::Decl *, std::string> decl_to_source_file_cache_;
47   std::map<std::string, uint64_t> qual_type_to_type_id_cache_;
48   uint64_t max_type_id_ = 0;
49 };
50 
51 
52 }  // namespace dumper
53 }  // namespace header_checker
54 
55 
56 #endif  // AST_UTIL_H_
57