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 #ifndef AST_UTIL 15 #define AST_UTIL 16 17 #include <clang/AST/AST.h> 18 19 #include <map> 20 #include <string> 21 22 namespace ast_util { 23 24 constexpr static char type_id_prefix[] = "type-"; 25 26 struct ASTCaches { 27 28 ASTCaches(const std::string &translation_unit_source) 29 : translation_unit_source_(translation_unit_source) { }; 30 31 std::string GetTypeId(const std::string &qual_type) { 32 auto type_id_it = qual_type_to_type_id_cache_.find(qual_type); 33 if (type_id_it == qual_type_to_type_id_cache_.end()) { 34 qual_type_to_type_id_cache_.insert( 35 std::make_pair(qual_type, ++max_type_id_)); 36 return type_id_prefix + std::to_string(max_type_id_); 37 } 38 return type_id_prefix + std::to_string(type_id_it->second); 39 } 40 41 std::string translation_unit_source_; 42 std::set<std::string> type_cache_; 43 std::map<const clang::Decl *, std::string> decl_to_source_file_cache_; 44 std::map<std::string, uint64_t> qual_type_to_type_id_cache_; 45 uint64_t max_type_id_ = 0; 46 }; 47 48 } 49 #endif 50