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 #include "dumper/ast_util.h"
19 #include "dumper/header_checker.h"
20 
21 #include <clang/AST/AST.h>
22 #include <clang/AST/ASTConsumer.h>
23 #include <clang/AST/Mangle.h>
24 #include <clang/AST/RecursiveASTVisitor.h>
25 #include <clang/Frontend/CompilerInstance.h>
26 #include <clang/Lex/PPCallbacks.h>
27 
28 #include <set>
29 
30 
31 namespace header_checker {
32 namespace dumper {
33 
34 
35 class HeaderASTVisitor
36     : public clang::RecursiveASTVisitor<HeaderASTVisitor> {
37  public:
38   HeaderASTVisitor(const HeaderCheckerOptions &options,
39                    clang::MangleContext *mangle_contextp,
40                    clang::ASTContext *ast_contextp,
41                    const clang::CompilerInstance *compiler_instance_p,
42                    const clang::Decl *tu_decl,
43                    repr::ModuleIR *module,
44                    ASTCaches *ast_caches);
45 
46   bool VisitRecordDecl(const clang::RecordDecl *decl);
47 
48   bool VisitFunctionDecl(const clang::FunctionDecl *decl);
49 
50   bool VisitEnumDecl(const clang::EnumDecl *decl);
51 
52   bool VisitVarDecl(const clang::VarDecl *decl);
53 
54   bool TraverseDecl(clang::Decl *decl);
55 
56   // Enable recursive traversal of template instantiations.
shouldVisitTemplateInstantiations()57   bool shouldVisitTemplateInstantiations() const { return true; }
58 
59  private:
60   bool ShouldSkipFunctionDecl(const clang::FunctionDecl *decl);
61 
62   const HeaderCheckerOptions &options_;
63   clang::MangleContext *mangle_contextp_;
64   clang::ASTContext *ast_contextp_;
65   const clang::CompilerInstance *cip_;
66   // To optimize recursion into only exported abi.
67   const clang::Decl *tu_decl_;
68   repr::ModuleIR *module_;
69   // We cache the source file an AST node corresponds to, to avoid repeated
70   // calls to "realpath".
71   ASTCaches *ast_caches_;
72 };
73 
74 
75 class HeaderASTConsumer : public clang::ASTConsumer {
76  public:
77   HeaderASTConsumer(clang::CompilerInstance *compiler_instancep,
78                     HeaderCheckerOptions &options);
79 
80   void HandleTranslationUnit(clang::ASTContext &ctx) override;
81 
82  private:
83   clang::CompilerInstance *cip_;
84   HeaderCheckerOptions &options_;
85 };
86 
87 
88 }  // namespace dumper
89 }  // namespace header_checker
90 
91 
92 #endif  // AST_PROCESSING_H_
93