Home | History | Annotate | Download | only in clang
      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::ASTContext &C;
     54   clang::DiagnosticsEngine &diag_;
     55   clang::Rewriter &rewriter_;
     56   llvm::raw_ostream &out_;
     57 };
     58 
     59 class TracepointTypeConsumer : public clang::ASTConsumer {
     60  public:
     61   explicit TracepointTypeConsumer(clang::ASTContext &C,
     62                                   clang::Rewriter &rewriter);
     63   bool HandleTopLevelDecl(clang::DeclGroupRef Group) override;
     64  private:
     65   TracepointTypeVisitor visitor_;
     66 };
     67 
     68 class TracepointFrontendAction : public clang::ASTFrontendAction {
     69  public:
     70   TracepointFrontendAction(llvm::raw_ostream &os);
     71 
     72   void EndSourceFileAction() override;
     73 
     74   std::unique_ptr<clang::ASTConsumer>
     75       CreateASTConsumer(clang::CompilerInstance &Compiler, llvm::StringRef InFile) override;
     76 
     77  private:
     78   llvm::raw_ostream &os_;
     79   std::unique_ptr<clang::Rewriter> rewriter_;
     80 };
     81 
     82 }  // namespace visitor
     83