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 frontend action.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_EXTRACTAPI_FRONTEND_ACTIONS_H
15 #define LLVM_CLANG_EXTRACTAPI_FRONTEND_ACTIONS_H
16 
17 #include "clang/ExtractAPI/API.h"
18 #include "clang/Frontend/FrontendAction.h"
19 
20 namespace clang {
21 
22 /// ExtractAPIAction sets up the output file and creates the ExtractAPIVisitor.
23 class ExtractAPIAction : public ASTFrontendAction {
24 protected:
25   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
26                                                  StringRef InFile) override;
27 
28 private:
29   /// A representation of the APIs this action extracts.
30   std::unique_ptr<extractapi::APISet> API;
31 
32   /// A stream to the output file of this action.
33   std::unique_ptr<raw_pwrite_stream> OS;
34 
35   /// The product this action is extracting API information for.
36   std::string ProductName;
37 
38   /// The synthesized input buffer that contains all the provided input header
39   /// files.
40   std::unique_ptr<llvm::MemoryBuffer> Buffer;
41 
42   /// The input file originally provided on the command line.
43   ///
44   /// This captures the spelling used to include the file and whether the
45   /// include is quoted or not.
46   SmallVector<std::pair<SmallString<32>, bool>> KnownInputFiles;
47 
48   /// Prepare to execute the action on the given CompilerInstance.
49   ///
50   /// This is called before executing the action on any inputs. This generates a
51   /// single header that includes all of CI's inputs and replaces CI's input
52   /// list with it before actually executing the action.
53   bool PrepareToExecuteAction(CompilerInstance &CI) override;
54 
55   /// Called after executing the action on the synthesized input buffer.
56   ///
57   /// Note: Now that we have gathered all the API definitions to surface we can
58   /// emit them in this callback.
59   void EndSourceFileAction() override;
60 
61   static std::unique_ptr<llvm::raw_pwrite_stream>
62   CreateOutputFile(CompilerInstance &CI, StringRef InFile);
63 
64   static StringRef getInputBufferName() { return "<extract-api-includes>"; }
65 };
66 
67 } // namespace clang
68 
69 #endif // LLVM_CLANG_EXTRACTAPI_FRONTEND_ACTIONS_H
70