1 //===- ClangSrcLocDump.cpp ------------------------------------*- 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 #include "clang/Basic/Diagnostic.h"
10 #include "clang/Driver/Compilation.h"
11 #include "clang/Driver/Driver.h"
12 #include "clang/Driver/Job.h"
13 #include "clang/Driver/Options.h"
14 #include "clang/Driver/Tool.h"
15 #include "clang/Frontend/CompilerInstance.h"
16 #include "clang/Frontend/TextDiagnosticPrinter.h"
17 #include "clang/Lex/PreprocessorOptions.h"
18 #include "clang/Tooling/Tooling.h"
19 #include "llvm/Option/ArgList.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/Host.h"
22 #include "llvm/Support/JSON.h"
23 
24 #include "ASTSrcLocProcessor.h"
25 
26 using namespace clang::tooling;
27 using namespace clang;
28 using namespace llvm;
29 
30 static cl::list<std::string> IncludeDirectories(
31     "I", cl::desc("Include directories to use while compiling"),
32     cl::value_desc("directory"), cl::Required, cl::OneOrMore, cl::Prefix);
33 
34 static cl::opt<bool>
35     SkipProcessing("skip-processing",
36                    cl::desc("Avoid processing the AST header file"),
37                    cl::Required, cl::value_desc("bool"));
38 
39 static cl::opt<std::string> JsonOutputPath("json-output-path",
40                                            cl::desc("json output path"),
41                                            cl::Required,
42                                            cl::value_desc("path"));
43 
44 class ASTSrcLocGenerationAction : public clang::ASTFrontendAction {
45 public:
ASTSrcLocGenerationAction()46   ASTSrcLocGenerationAction() : Processor(JsonOutputPath) {}
47 
ExecuteAction()48   void ExecuteAction() override {
49     clang::ASTFrontendAction::ExecuteAction();
50     if (getCompilerInstance().getDiagnostics().getNumErrors() > 0)
51       Processor.generateEmpty();
52     else
53       Processor.generate();
54   }
55 
56   std::unique_ptr<clang::ASTConsumer>
CreateASTConsumer(clang::CompilerInstance & Compiler,llvm::StringRef File)57   CreateASTConsumer(clang::CompilerInstance &Compiler,
58                     llvm::StringRef File) override {
59     return Processor.createASTConsumer(Compiler, File);
60   }
61 
62 private:
63   ASTSrcLocProcessor Processor;
64 };
65 
66 static const char Filename[] = "ASTTU.cpp";
67 
main(int argc,const char ** argv)68 int main(int argc, const char **argv) {
69 
70   cl::ParseCommandLineOptions(argc, argv);
71 
72   if (SkipProcessing) {
73     std::error_code EC;
74     llvm::raw_fd_ostream JsonOut(JsonOutputPath, EC, llvm::sys::fs::OF_Text);
75     if (EC)
76       return 1;
77     JsonOut << formatv("{0:2}", llvm::json::Value(llvm::json::Object()));
78     return 0;
79   }
80 
81   std::vector<std::string> Args;
82   Args.push_back("-cc1");
83 
84   llvm::transform(IncludeDirectories, std::back_inserter(Args),
85                   [](const std::string &IncDir) { return "-I" + IncDir; });
86 
87   Args.push_back("-fsyntax-only");
88   Args.push_back(Filename);
89 
90   std::vector<const char *> Argv(Args.size(), nullptr);
91   llvm::transform(Args, Argv.begin(),
92                   [](const std::string &Arg) { return Arg.c_str(); });
93 
94   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
95   unsigned MissingArgIndex, MissingArgCount;
96   auto Opts = driver::getDriverOptTable();
97   auto ParsedArgs = Opts.ParseArgs(llvm::makeArrayRef(Argv).slice(1),
98                                    MissingArgIndex, MissingArgCount);
99   ParseDiagnosticArgs(*DiagOpts, ParsedArgs);
100 
101   // Don't output diagnostics, because common scenarios such as
102   // cross-compiling fail with diagnostics.  This is not fatal, but
103   // just causes attempts to use the introspection API to return no data.
104   TextDiagnosticPrinter DiagnosticPrinter(llvm::nulls(), &*DiagOpts);
105   DiagnosticsEngine Diagnostics(
106       IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
107       &DiagnosticPrinter, false);
108 
109   auto *OFS = new llvm::vfs::OverlayFileSystem(vfs::getRealFileSystem());
110 
111   auto *MemFS = new llvm::vfs::InMemoryFileSystem();
112   OFS->pushOverlay(MemFS);
113   MemFS->addFile(Filename, 0,
114                  MemoryBuffer::getMemBuffer("#include \"clang/AST/AST.h\"\n"));
115 
116   auto Files = llvm::makeIntrusiveRefCnt<FileManager>(FileSystemOptions(), OFS);
117 
118   auto Driver = std::make_unique<driver::Driver>(
119       "clang", llvm::sys::getDefaultTargetTriple(), Diagnostics,
120       "ast-api-dump-tool", OFS);
121 
122   std::unique_ptr<clang::driver::Compilation> Comp(
123       Driver->BuildCompilation(llvm::makeArrayRef(Argv)));
124   if (!Comp)
125     return 1;
126 
127   const auto &Jobs = Comp->getJobs();
128   if (Jobs.size() != 1 || !isa<driver::Command>(*Jobs.begin())) {
129     SmallString<256> error_msg;
130     llvm::raw_svector_ostream error_stream(error_msg);
131     Jobs.Print(error_stream, "; ", true);
132     return 1;
133   }
134 
135   const auto &Cmd = cast<driver::Command>(*Jobs.begin());
136   const llvm::opt::ArgStringList &CC1Args = Cmd.getArguments();
137 
138   auto Invocation = std::make_unique<CompilerInvocation>();
139   CompilerInvocation::CreateFromArgs(*Invocation, CC1Args, Diagnostics);
140 
141   CompilerInstance Compiler(std::make_shared<clang::PCHContainerOperations>());
142   Compiler.setInvocation(std::move(Invocation));
143 
144   Compiler.createDiagnostics(&DiagnosticPrinter, false);
145   if (!Compiler.hasDiagnostics())
146     return 1;
147 
148   // Suppress "2 errors generated" or similar messages
149   Compiler.getDiagnosticOpts().ShowCarets = false;
150   Compiler.createSourceManager(*Files);
151   Compiler.setFileManager(Files.get());
152 
153   ASTSrcLocGenerationAction ScopedToolAction;
154   Compiler.ExecuteAction(ScopedToolAction);
155 
156   Files->clearStatCache();
157 
158   return 0;
159 }
160