xref: /aosp_15_r20/external/bcc/src/cc/frontends/clang/tp_frontend_action.h (revision 387f9dfdfa2baef462e92476d413c7bc2470293e)
1 /*
2  * Copyright (c) 2016 Sasha Goldshtein
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 <map>
18 #include <memory>
19 #include <set>
20 #include <string>
21 #include <vector>
22 
23 #include <clang/AST/RecursiveASTVisitor.h>
24 #include <clang/Frontend/FrontendAction.h>
25 #include <clang/Rewrite/Core/Rewriter.h>
26 
27 namespace clang {
28 class ASTConsumer;
29 class ASTContext;
30 class CompilerInstance;
31 }
32 
33 namespace llvm {
34 class raw_ostream;
35 class StringRef;
36 }
37 
38 namespace ebpf {
39 
40 // Visit functions that have a tracepoint argument structure in their signature
41 // and automatically generate the structure on-the-fly.
42 class TracepointTypeVisitor :
43   public clang::RecursiveASTVisitor<TracepointTypeVisitor> {
44  public:
45   explicit TracepointTypeVisitor(clang::ASTContext &C,
46                                  clang::Rewriter &rewriter);
47   bool VisitFunctionDecl(clang::FunctionDecl *D);
48 
49  private:
50   std::string GenerateTracepointStruct(clang::SourceLocation loc,
51           std::string const& category, std::string const& event);
52 
53   clang::DiagnosticsEngine &diag_;
54   clang::Rewriter &rewriter_;
55   llvm::raw_ostream &out_;
56 };
57 
58 class TracepointTypeConsumer : public clang::ASTConsumer {
59  public:
60   explicit TracepointTypeConsumer(clang::ASTContext &C,
61                                   clang::Rewriter &rewriter);
62   bool HandleTopLevelDecl(clang::DeclGroupRef Group) override;
63  private:
64   TracepointTypeVisitor visitor_;
65 };
66 
67 class TracepointFrontendAction : public clang::ASTFrontendAction {
68  public:
69   TracepointFrontendAction(llvm::raw_ostream &os);
70 
71   void EndSourceFileAction() override;
72 
73   std::unique_ptr<clang::ASTConsumer>
74       CreateASTConsumer(clang::CompilerInstance &Compiler, llvm::StringRef InFile) override;
75 
76  private:
77   llvm::raw_ostream &os_;
78   std::unique_ptr<clang::Rewriter> rewriter_;
79 };
80 
81 }  // namespace visitor
82