1 // Copyright (C) 2016 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_PROCESSING_H_ 16 #define AST_PROCESSING_H_ 17 18 #pragma clang diagnostic push 19 #pragma clang diagnostic ignored "-Wunused-parameter" 20 #pragma clang diagnostic ignored "-Wnested-anon-types" 21 #include "proto/abi_dump.pb.h" 22 #pragma clang diagnostic pop 23 24 #include <clang/AST/AST.h> 25 #include <clang/AST/ASTConsumer.h> 26 #include <clang/AST/Mangle.h> 27 #include <clang/AST/RecursiveASTVisitor.h> 28 #include <clang/Frontend/CompilerInstance.h> 29 #include <clang/Lex/PPCallbacks.h> 30 31 #include <set> 32 33 class HeaderASTVisitor 34 : public clang::RecursiveASTVisitor<HeaderASTVisitor> { 35 public: 36 HeaderASTVisitor(abi_dump::TranslationUnit *tu_ptr, 37 clang::MangleContext *mangle_contextp, 38 clang::ASTContext *ast_contextp, 39 const clang::CompilerInstance *compiler_instance_p, 40 const std::string ¤t_file_name, 41 const std::set<std::string> &exported_headers, 42 const clang::Decl *tu_decl); 43 44 bool VisitRecordDecl(const clang::RecordDecl *decl); 45 46 bool VisitFunctionDecl(const clang::FunctionDecl *decl); 47 48 bool VisitEnumDecl(const clang::EnumDecl *decl); 49 50 bool VisitVarDecl(const clang::VarDecl *decl); 51 52 bool TraverseDecl(clang::Decl *decl); 53 54 // Enable recursive traversal of template instantiations. shouldVisitTemplateInstantiations()55 bool shouldVisitTemplateInstantiations() const { 56 return true; 57 } 58 59 private: 60 abi_dump::TranslationUnit *tu_ptr_; 61 clang::MangleContext *mangle_contextp_; 62 clang::ASTContext *ast_contextp_; 63 const clang::CompilerInstance *cip_; 64 const std::string current_file_name_; 65 const std::set<std::string> &exported_headers_; 66 // To optimize recursion into only exported abi. 67 const clang::Decl *tu_decl_; 68 }; 69 70 class HeaderASTConsumer : public clang::ASTConsumer { 71 public: 72 HeaderASTConsumer(const std::string &file_name, 73 clang::CompilerInstance *compiler_instancep, 74 const std::string &out_dump_name, 75 const std::set<std::string> &exported_headers); 76 77 void HandleTranslationUnit(clang::ASTContext &ctx) override; 78 79 private: 80 std::string file_name_; 81 clang::CompilerInstance *cip_; 82 std::string out_dump_name_; 83 std::set<std::string> exported_headers_; 84 }; 85 86 #endif // AST_PROCESSING_H_ 87