1 //===- ExtractAPI/FrontendActions.h -----------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file defines the ExtractAPIAction and WrappingExtractAPIAction frontend
11 /// actions.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_EXTRACTAPI_FRONTEND_ACTIONS_H
16 #define LLVM_CLANG_EXTRACTAPI_FRONTEND_ACTIONS_H
17 
18 #include "clang/ExtractAPI/ExtractAPIActionBase.h"
19 #include "clang/Frontend/FrontendAction.h"
20 
21 namespace clang {
22 
23 /// ExtractAPIAction sets up the output file and creates the ExtractAPIVisitor.
24 class ExtractAPIAction : public ASTFrontendAction,
25                          private ExtractAPIActionBase {
26 protected:
27   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
28                                                  StringRef InFile) override;
29 
30 private:
31 
32   /// The input file originally provided on the command line.
33   ///
34   /// This captures the spelling used to include the file and whether the
35   /// include is quoted or not.
36   SmallVector<std::pair<SmallString<32>, bool>> KnownInputFiles;
37 
38   /// Prepare to execute the action on the given CompilerInstance.
39   ///
40   /// This is called before executing the action on any inputs. This generates a
41   /// single header that includes all of CI's inputs and replaces CI's input
42   /// list with it before actually executing the action.
43   bool PrepareToExecuteAction(CompilerInstance &CI) override;
44 
45   /// Called after executing the action on the synthesized input buffer.
46   ///
47   /// Note: Now that we have gathered all the API definitions to surface we can
48   /// emit them in this callback.
49   void EndSourceFileAction() override;
50 
51   static StringRef getInputBufferName() { return "<extract-api-includes>"; }
52 
53   static std::unique_ptr<llvm::raw_pwrite_stream>
54   CreateOutputFile(CompilerInstance &CI, StringRef InFile);
55 };
56 
57 /// Wrap ExtractAPIAction on top of a pre-existing action
58 ///
59 /// Used when the ExtractAPI action needs to be executed as a side effect of a
60 /// regular compilation job. Unlike ExtarctAPIAction, this is meant to be used
61 /// on regular source files ( .m , .c files) instead of header files
62 class WrappingExtractAPIAction : public WrapperFrontendAction,
63                                  private ExtractAPIActionBase {
64 public:
65   WrappingExtractAPIAction(std::unique_ptr<FrontendAction> WrappedAction)
66       : WrapperFrontendAction(std::move(WrappedAction)) {}
67 
68 protected:
69   /// Create ExtractAPI consumer multiplexed on another consumer.
70   ///
71   /// This allows us to execute ExtractAPI action while on top of
72   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
73                                                  StringRef InFile) override;
74 
75 private:
76   /// Flag to check if the wrapper front end action's consumer is
77   /// craeted or not
78   bool CreatedASTConsumer = false;
79 
80   void EndSourceFile() override { FrontendAction::EndSourceFile(); }
81 
82   /// Called after executing the action on the synthesized input buffer.
83   ///
84   /// Executes both Wrapper and ExtractAPIBase end source file
85   /// actions. This is the place where all the gathered symbol graph
86   /// information is emited.
87   void EndSourceFileAction() override;
88 
89   static std::unique_ptr<llvm::raw_pwrite_stream>
90   CreateOutputFile(CompilerInstance &CI, StringRef InFile);
91 };
92 
93 } // namespace clang
94 
95 #endif // LLVM_CLANG_EXTRACTAPI_FRONTEND_ACTIONS_H
96