clang 19.0.0git
ClangSrcLocDump.cpp
Go to the documentation of this file.
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
11#include "clang/Driver/Driver.h"
12#include "clang/Driver/Job.h"
14#include "clang/Driver/Tool.h"
19#include "llvm/Option/ArgList.h"
20#include "llvm/Support/CommandLine.h"
21#include "llvm/Support/JSON.h"
22#include "llvm/TargetParser/Host.h"
23
24#include "ASTSrcLocProcessor.h"
25
26using namespace clang::tooling;
27using namespace clang;
28using namespace llvm;
29
30static 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
34static cl::opt<bool>
35 SkipProcessing("skip-processing",
36 cl::desc("Avoid processing the AST header file"),
37 cl::Required, cl::value_desc("bool"));
38
39static cl::opt<std::string> JsonOutputPath("json-output-path",
40 cl::desc("json output path"),
41 cl::Required,
42 cl::value_desc("path"));
43
45public:
47
48 void ExecuteAction() override {
50 if (getCompilerInstance().getDiagnostics().getNumErrors() > 0)
51 Processor.generateEmpty();
52 else
53 Processor.generate();
54 }
55
56 std::unique_ptr<clang::ASTConsumer>
58 llvm::StringRef File) override {
59 return Processor.createASTConsumer(Compiler, File);
60 }
61
62private:
63 ASTSrcLocProcessor Processor;
64};
65
66static const char Filename[] = "ASTTU.cpp";
67
68int 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
96
97 // Don't output diagnostics, because common scenarios such as
98 // cross-compiling fail with diagnostics. This is not fatal, but
99 // just causes attempts to use the introspection API to return no data.
100 TextDiagnosticPrinter DiagnosticPrinter(llvm::nulls(), &*DiagOpts);
101 DiagnosticsEngine Diagnostics(
103 &DiagnosticPrinter, false);
104
105 auto *OFS = new llvm::vfs::OverlayFileSystem(vfs::getRealFileSystem());
106
107 auto *MemFS = new llvm::vfs::InMemoryFileSystem();
108 OFS->pushOverlay(MemFS);
109 MemFS->addFile(Filename, 0,
110 MemoryBuffer::getMemBuffer("#include \"clang/AST/AST.h\"\n"));
111
112 auto Files = llvm::makeIntrusiveRefCnt<FileManager>(FileSystemOptions(), OFS);
113
114 auto Driver = std::make_unique<clang::driver::Driver>(
115 "clang", llvm::sys::getDefaultTargetTriple(), Diagnostics,
116 "ast-api-dump-tool", OFS);
117
118 std::unique_ptr<clang::driver::Compilation> Comp(
119 Driver->BuildCompilation(llvm::ArrayRef(Argv)));
120 if (!Comp)
121 return 1;
122
123 const auto &Jobs = Comp->getJobs();
124 if (Jobs.size() != 1 || !isa<clang::driver::Command>(*Jobs.begin())) {
125 SmallString<256> error_msg;
126 llvm::raw_svector_ostream error_stream(error_msg);
127 Jobs.Print(error_stream, "; ", true);
128 return 1;
129 }
130
131 const auto &Cmd = cast<clang::driver::Command>(*Jobs.begin());
132 const llvm::opt::ArgStringList &CC1Args = Cmd.getArguments();
133
134 auto Invocation = std::make_unique<CompilerInvocation>();
135 CompilerInvocation::CreateFromArgs(*Invocation, CC1Args, Diagnostics);
136
137 CompilerInstance Compiler(std::make_shared<clang::PCHContainerOperations>());
138 Compiler.setInvocation(std::move(Invocation));
139
140 Compiler.createDiagnostics(&DiagnosticPrinter, false);
141 if (!Compiler.hasDiagnostics())
142 return 1;
143
144 // Suppress "2 errors generated" or similar messages
145 Compiler.getDiagnosticOpts().ShowCarets = false;
146 Compiler.createSourceManager(*Files);
147 Compiler.setFileManager(Files.get());
148
149 ASTSrcLocGenerationAction ScopedToolAction;
150 Compiler.ExecuteAction(ScopedToolAction);
151
152 Files->clearStatCache();
153
154 return 0;
155}
Defines the Diagnostic-related interfaces.
int main(int argc, const char **argv)
static cl::opt< std::string > JsonOutputPath("json-output-path", cl::desc("json output path"), cl::Required, cl::value_desc("path"))
static const char Filename[]
static cl::list< std::string > IncludeDirectories("I", cl::desc("Include directories to use while compiling"), cl::value_desc("directory"), cl::Required, cl::OneOrMore, cl::Prefix)
static cl::opt< bool > SkipProcessing("skip-processing", cl::desc("Avoid processing the AST header file"), cl::Required, cl::value_desc("bool"))
CompileCommand Cmd
std::unique_ptr< clang::ASTConsumer > CreateASTConsumer(clang::CompilerInstance &Compiler, llvm::StringRef File) override
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer.
Abstract base class to use for AST consumer-based frontend actions.
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
void setFileManager(FileManager *Value)
Replace the current file manager and virtual file system.
void createSourceManager(FileManager &FileMgr)
Create the source manager and replace any existing one with it.
void createDiagnostics(DiagnosticConsumer *Client=nullptr, bool ShouldOwnClient=true)
Create the diagnostics engine using the invocation's diagnostic options and replace any existing one ...
void setInvocation(std::shared_ptr< CompilerInvocation > Value)
setInvocation - Replace the current invocation.
bool ExecuteAction(FrontendAction &Act)
ExecuteAction - Execute the provided action against the compiler's CompilerInvocation object.
DiagnosticOptions & getDiagnosticOpts()
static bool CreateFromArgs(CompilerInvocation &Res, ArrayRef< const char * > CommandLineArgs, DiagnosticsEngine &Diags, const char *Argv0=nullptr)
Create a compiler invocation from a list of input options.
Used for handling and querying diagnostic IDs.
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:192
Keeps track of options that affect how file operations are performed.
CompilerInstance & getCompilerInstance() const
std::unique_ptr< ASTConsumer > createASTConsumer(CompilerInstance &Compiler, StringRef File)
The JSON file list parser is used to communicate input to InstallAPI.
std::unique_ptr< DiagnosticOptions > CreateAndPopulateDiagOpts(ArrayRef< const char * > Argv)
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30