clang  14.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"
13 #include "clang/Driver/Options.h"
14 #include "clang/Driver/Tool.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 
45 public:
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 
62 private:
63  ASTSrcLocProcessor Processor;
64 };
65 
66 static const char Filename[] = "ASTTU.cpp";
67 
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 
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<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::makeArrayRef(Argv)));
120  if (!Comp)
121  return 1;
122 
123  const auto &Jobs = Comp->getJobs();
124  if (Jobs.size() != 1 || !isa<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<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 }
llvm
Definition: Dominators.h:30
clang::DeclaratorContext::File
@ File
Driver.h
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::DiagnosticsEngine
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:191
Job.h
ASTSrcLocGenerationAction
Definition: ClangSrcLocDump.cpp:44
main
int main(int argc, const char **argv)
Definition: ClangSrcLocDump.cpp:68
clang::FileSystemOptions
Keeps track of options that affect how file operations are performed.
Definition: FileSystemOptions.h:22
clang::CompilerInstance::createDiagnostics
void createDiagnostics(DiagnosticConsumer *Client=nullptr, bool ShouldOwnClient=true)
Create the diagnostics engine using the invocation's diagnostic options and replace any existing one ...
Definition: CompilerInstance.cpp:329
Options.h
clang::CompilerInstance::ExecuteAction
bool ExecuteAction(FrontendAction &Act)
ExecuteAction - Execute the provided action against the compiler's CompilerInvocation object.
Definition: CompilerInstance.cpp:989
Diagnostic.h
ASTSrcLocGenerationAction::ASTSrcLocGenerationAction
ASTSrcLocGenerationAction()
Definition: ClangSrcLocDump.cpp:46
ASTSrcLocGenerationAction::CreateASTConsumer
std::unique_ptr< clang::ASTConsumer > CreateASTConsumer(clang::CompilerInstance &Compiler, llvm::StringRef File) override
Definition: ClangSrcLocDump.cpp:57
clang::tooling
Definition: AllTUsExecution.h:21
Tool.h
llvm::SmallString
Definition: LLVM.h:37
SkipProcessing
static cl::opt< bool > SkipProcessing("skip-processing", cl::desc("Avoid processing the AST header file"), cl::Required, cl::value_desc("bool"))
clang::CompilerInstance
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Definition: CompilerInstance.h:74
Cmd
CompileCommand Cmd
Definition: InterpolatingCompilationDatabase.cpp:130
clang::CompilerInstance::setInvocation
void setInvocation(std::shared_ptr< CompilerInvocation > Value)
setInvocation - Replace the current invocation.
Definition: CompilerInstance.cpp:72
JsonOutputPath
static cl::opt< std::string > JsonOutputPath("json-output-path", cl::desc("json output path"), cl::Required, cl::value_desc("path"))
Tooling.h
Compilation.h
IncludeDirectories
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)
TextDiagnosticPrinter.h
Value
Value
Definition: UninitializedValues.cpp:102
clang::CompilerInstance::hasDiagnostics
bool hasDiagnostics() const
Definition: CompilerInstance.h:333
clang::DiagnosticIDs
Used for handling and querying diagnostic IDs.
Definition: DiagnosticIDs.h:166
ASTSrcLocProcessor.h
clang::CompilerInstance::getDiagnosticOpts
DiagnosticOptions & getDiagnosticOpts()
Definition: CompilerInstance.h:270
clang
Definition: CalledOnceCheck.h:17
clang::TextDiagnosticPrinter
Definition: TextDiagnosticPrinter.h:27
clang::CompilerInstance::setFileManager
void setFileManager(FileManager *Value)
Replace the current file manager and virtual file system.
Definition: CompilerInstance.cpp:161
clang::CreateAndPopulateDiagOpts
std::unique_ptr< DiagnosticOptions > CreateAndPopulateDiagOpts(ArrayRef< const char * > Argv)
Definition: CompilerInvocation.cpp:2285
clang::ASTFrontendAction::ExecuteAction
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer.
Definition: FrontendAction.cpp:1050
CompilerInstance.h
Filename
static const char Filename[]
Definition: ClangSrcLocDump.cpp:66
clang::ASTFrontendAction
Abstract base class to use for AST consumer-based frontend actions.
Definition: FrontendAction.h:243
ASTSrcLocGenerationAction::ExecuteAction
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer.
Definition: ClangSrcLocDump.cpp:48
PreprocessorOptions.h
clang::CompilerInstance::createSourceManager
void createSourceManager(FileManager &FileMgr)
Create the source manager and replace any existing one with it.
Definition: CompilerInstance.cpp:384
clang::CompilerInvocation::CreateFromArgs
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.
Definition: CompilerInvocation.cpp:4525
llvm::IntrusiveRefCntPtr
Definition: LLVM.h:47
clang::tooling::ASTSrcLocProcessor
Definition: ASTSrcLocProcessor.h:25