23 #include "llvm/Support/FileSystem.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/Path.h"
26 #include "llvm/Support/raw_ostream.h"
28 #include <system_error>
30 using namespace clang;
36 std::unique_ptr<ASTConsumer>
38 return llvm::make_unique<ASTConsumer>();
41 void InitOnlyAction::ExecuteAction() {
48 std::unique_ptr<ASTConsumer>
50 if (std::unique_ptr<raw_ostream> OS =
56 std::unique_ptr<ASTConsumer>
64 std::unique_ptr<ASTConsumer>
69 std::unique_ptr<ASTConsumer>
74 std::unique_ptr<ASTConsumer>
80 std::unique_ptr<ASTConsumer>
83 std::string OutputFile;
84 std::unique_ptr<raw_pwrite_stream> OS =
92 auto Buffer = std::make_shared<PCHBuffer>();
93 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
94 Consumers.push_back(llvm::make_unique<PCHGenerator>(
101 CI, InFile, OutputFile, std::move(OS), Buffer));
103 return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
106 std::unique_ptr<raw_pwrite_stream>
109 std::string &Sysroot,
110 std::string &OutputFile) {
120 std::unique_ptr<raw_pwrite_stream> OS =
142 std::unique_ptr<ASTConsumer>
145 std::unique_ptr<raw_pwrite_stream> OS = CreateOutputFile(CI, InFile);
152 auto Buffer = std::make_shared<PCHBuffer>();
153 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
155 Consumers.push_back(llvm::make_unique<PCHGenerator>(
162 CI, InFile, OutputFile, std::move(OS), Buffer));
163 return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
166 bool GenerateModuleFromModuleMapAction::BeginSourceFileAction(
176 std::unique_ptr<raw_pwrite_stream>
183 if (ModuleMapFile.empty())
184 ModuleMapFile = InFile;
201 bool GenerateModuleInterfaceAction::BeginSourceFileAction(
213 std::unique_ptr<raw_pwrite_stream>
222 std::unique_ptr<ASTConsumer>
224 return llvm::make_unique<ASTConsumer>();
227 std::unique_ptr<ASTConsumer>
230 return llvm::make_unique<ASTConsumer>();
233 std::unique_ptr<ASTConsumer>
235 return llvm::make_unique<ASTConsumer>();
242 std::unique_ptr<ASTReader> Reader(
new ASTReader(
245 Sysroot.empty() ?
"" : Sysroot.c_str(),
262 llvm::raw_ostream &Out;
265 DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { }
267 #define DUMP_BOOLEAN(Value, Text) \
268 Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n"
270 bool ReadFullVersionInformation(StringRef FullVersion)
override {
275 <<
" Clang: " << FullVersion <<
"\n";
279 void ReadModuleName(StringRef ModuleName)
override {
280 Out.indent(2) <<
"Module name: " << ModuleName <<
"\n";
282 void ReadModuleMapFile(StringRef ModuleMapPath)
override {
283 Out.indent(2) <<
"Module map file: " << ModuleMapPath <<
"\n";
286 bool ReadLanguageOptions(
const LangOptions &LangOpts,
bool Complain,
287 bool AllowCompatibleDifferences)
override {
288 Out.indent(2) <<
"Language options:\n";
289 #define LANGOPT(Name, Bits, Default, Description) \
290 DUMP_BOOLEAN(LangOpts.Name, Description);
291 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
292 Out.indent(4) << Description << ": " \
293 << static_cast<unsigned>(LangOpts.get##Name()) << "\n";
294 #define VALUE_LANGOPT(Name, Bits, Default, Description) \
295 Out.indent(4) << Description << ": " << LangOpts.Name << "\n";
296 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
297 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
298 #include "clang/Basic/LangOptions.def"
301 Out.indent(4) <<
"Module features:\n";
303 Out.indent(6) << Feature <<
"\n";
309 bool ReadTargetOptions(
const TargetOptions &TargetOpts,
bool Complain,
310 bool AllowCompatibleDifferences)
override {
311 Out.indent(2) <<
"Target options:\n";
312 Out.indent(4) <<
" Triple: " << TargetOpts.
Triple <<
"\n";
313 Out.indent(4) <<
" CPU: " << TargetOpts.
CPU <<
"\n";
314 Out.indent(4) <<
" ABI: " << TargetOpts.
ABI <<
"\n";
317 Out.indent(4) <<
"Target features:\n";
328 bool Complain)
override {
329 Out.indent(2) <<
"Diagnostic options:\n";
330 #define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name);
331 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
332 Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n";
333 #define VALUE_DIAGOPT(Name, Bits, Default) \
334 Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n";
335 #include "clang/Basic/DiagnosticOptions.def"
337 Out.indent(4) <<
"Diagnostic flags:\n";
338 for (
const std::string &
Warning : DiagOpts->Warnings)
339 Out.indent(6) <<
"-W" << Warning <<
"\n";
340 for (
const std::string &
Remark : DiagOpts->Remarks)
341 Out.indent(6) <<
"-R" << Remark <<
"\n";
347 StringRef SpecificModuleCachePath,
348 bool Complain)
override {
349 Out.indent(2) <<
"Header search options:\n";
350 Out.indent(4) <<
"System root [-isysroot=]: '" << HSOpts.
Sysroot <<
"'\n";
351 Out.indent(4) <<
"Resource dir [ -resource-dir=]: '" << HSOpts.
ResourceDir <<
"'\n";
352 Out.indent(4) <<
"Module Cache: '" << SpecificModuleCachePath <<
"'\n";
354 "Use builtin include directories [-nobuiltininc]");
356 "Use standard system include directories [-nostdinc]");
358 "Use standard C++ include directories [-nostdinc++]");
360 "Use libc++ (rather than libstdc++) [-stdlib=]");
366 std::string &SuggestedPredefines)
override {
367 Out.indent(2) <<
"Preprocessor options:\n";
369 "Uses compiler/target-specific predefines [-undef]");
371 "Uses detailed preprocessing record (for indexing)");
373 if (!PPOpts.
Macros.empty()) {
374 Out.indent(4) <<
"Predefined macros:\n";
377 for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
385 Out <<
I->first <<
"\n";
391 void readModuleFileExtension(
393 Out.indent(2) <<
"Module file extension '"
398 Out.write_escaped(Metadata.
UserInfo);
416 std::unique_ptr<llvm::raw_fd_ostream> OutFile;
418 if (!OutputFileName.empty() && OutputFileName !=
"-") {
420 OutFile.reset(
new llvm::raw_fd_ostream(OutputFileName.str(), EC,
421 llvm::sys::fs::F_Text));
423 llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs();
425 Out <<
"Information for module file '" <<
getCurrentFile() <<
"':\n";
428 StringRef Magic = (*Buffer)->getMemBufferRef().getBuffer();
429 bool IsRaw = (Magic.size() >= 4 && Magic[0] ==
'C' && Magic[1] ==
'P' &&
430 Magic[2] ==
'C' && Magic[3] ==
'H');
431 Out <<
" Module format: " << (IsRaw ?
"raw" :
"obj") <<
"\n";
434 DumpModuleInfoListener Listener(Out);
436 PP.getHeaderSearchInfo().getHeaderSearchOpts();
457 RawLex.LexFromRawLexer(RawTok);
460 llvm::errs() <<
"\n";
461 RawLex.LexFromRawLexer(RawTok);
473 llvm::errs() <<
"\n";
479 std::unique_ptr<raw_pwrite_stream> OS =
516 bool BinaryMode =
true;
517 bool InvalidFile =
false;
522 const char *cur = Buffer->getBufferStart();
523 const char *end = Buffer->getBufferEnd();
524 const char *next = (cur != end) ? cur + 1 : end;
529 if (end - cur > 256) end = cur + 256;
537 }
else if (*cur == 0x0A)
545 std::unique_ptr<raw_ostream> OS =
553 if (
Input.isFile()) {
555 OS->write_escaped(
Input.getFile());
561 (*OS) <<
"#pragma clang module contents\n";
595 llvm::outs().write((*Buffer)->getBufferStart(), Preamble);
std::string OutputFile
The output file, if any.
SourceManager & getSourceManager() const
LangOptions & getLangOpts()
ASTContext & getASTContext() const
std::string OriginalModuleMap
When the input is a module map, the original module map file from which that map was inferred...
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
PreprocessorOptions & getPreprocessorOpts()
bool shouldEraseOutputFiles() override
Callback at the end of processing a single input, to determine if the output files should be erased o...
std::vector< std::pair< std::string, bool > > Macros
Lexer - This provides a simple interface that turns a text buffer into a stream of tokens...
static std::pair< unsigned, bool > ComputePreamble(StringRef Buffer, const LangOptions &LangOpts, unsigned MaxLines=0)
Compute the preamble of the given file.
Defines the clang::FileManager interface and associated types.
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
CompilerInstance & getCompilerInstance() const
llvm::MemoryBuffer * getBuffer(FileID FID, SourceLocation Loc, bool *Invalid=nullptr) const
Return the buffer for the specified FileID.
std::unique_ptr< llvm::MemoryBuffer > Buffer
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
static std::unique_ptr< raw_pwrite_stream > ComputeASTConsumerArguments(CompilerInstance &CI, StringRef InFile, std::string &Sysroot, std::string &OutputFile)
Compute the AST consumer arguments that will be used to create the PCHGenerator instance returned by ...
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
std::string ASTDumpFilter
If given, filter dumped AST Decl nodes by this substring.
Compiling a C++ modules TS module interface unit.
SourceManager & getSourceManager() const
Return the current source manager.
void IgnorePragmas()
Install empty handlers for all pragmas (making them ignored).
Options for controlling the target.
InputKind getCurrentFileKind() const
void DumpToken(const Token &Tok, bool DumpFlags=false) const
Print the token to stderr, used for debugging.
std::unique_ptr< ASTConsumer > CreateASTDumper(StringRef FilterString, bool DumpDecls, bool Deserialize, bool DumpLookups)
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
const FrontendInputFile & getCurrentInput() const
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
const LangOptions & getLangOpts() const
Token - This structure provides full information about a lexed token.
unsigned DetailedRecord
Whether we should maintain a detailed record of all macro definitions and expansions.
The client can handle an AST file that cannot load because it's compiled configuration doesn't match ...
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
unsigned BuildingImplicitModule
Whether we are performing an implicit module build.
bool BeginSourceFileAction(CompilerInstance &CI) override
Callback at the start of processing a single input.
unsigned RelocatablePCH
When generating PCH files, instruct the AST writer to create relocatable PCH files.
FrontendOptions & getFrontendOpts()
PreprocessorOutputOptions & getPreprocessorOutputOpts()
HeaderSearch & getHeaderSearchInfo() const
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
Module * getCurrentModule() const
void CacheTokens(Preprocessor &PP, raw_pwrite_stream *OS)
Cache tokens for use with PCH. Note that this requires a seekable stream.
bool BeginInvocation(CompilerInstance &CI) override
Callback before starting processing a single input, giving the opportunity to modify the CompilerInvo...
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
#define DUMP_BOOLEAN(Value, Text)
HeaderSearchOptions & getHeaderSearchOpts()
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer...
detail::InMemoryDirectory::const_iterator I
Preprocessor & getPreprocessor() const
Return the current preprocessor.
std::vector< std::string > ModuleFeatures
The names of any features to enable in module 'requires' decls in addition to the hard-coded list in ...
std::string CurrentModule
The name of the current module, of which the main source file is a part.
virtual bool shouldEraseOutputFiles()
Callback at the end of processing a single input, to determine if the output files should be erased o...
unsigned ASTDumpAll
Whether we deserialize all decls when forming AST dumps.
unsigned UsePredefines
Initialize the preprocessor with the compiler and target specific predefines.
void DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS, const PreprocessorOutputOptions &Opts)
DoPrintPreprocessedInput - Implement -E mode.
void EnterMainSourceFile()
Enter the specified FileID as the main source file, which implicitly adds the builtin defines etc...
Defines the clang::Preprocessor interface.
bool isNot(tok::TokenKind K) const
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
std::unique_ptr< raw_pwrite_stream > createOutputFile(StringRef OutputPath, bool Binary, bool RemoveFileOnSignal, StringRef BaseInput, StringRef Extension, bool UseTemporary, bool CreateMissingDirectories=false)
Create a new output file and add it to the list of tracked output files, optionally deriving the outp...
bool AllowPCHWithCompilerErrors
When true, a PCH with compiler errors will not be rejected.
std::string CPU
If given, the name of the target CPU to generate code for.
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
std::string ABI
If given, the name of the target ABI to use.
File is a PCH file treated as the preamble.
void print(raw_ostream &OS, unsigned Indent=0) const
Print the module map for this module to the given stream.
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Encodes a location in the source.
const PCHContainerReader & getPCHContainerReader() const
Return the appropriate PCHContainerReader depending on the current CodeGenOptions.
File is a PCH file treated as such.
const StringRef getCurrentFile() const
unsigned IncludeTimestamps
Whether timestamps should be written to the produced PCH file.
std::string getClangFullRepositoryVersion()
Retrieves the full repository version that is an amalgamation of the information in getClangRepositor...
void Lex(Token &Result)
Lex the next token for this preprocessor.
virtual bool ReadFullVersionInformation(StringRef FullVersion)
Receives the full Clang version information.
unsigned ASTDumpLookups
Whether we include lookup table dumps in AST dumps.
std::vector< std::string > FeaturesAsWritten
The list of target specific features to enable or disable, as written on the command line...
FileID getMainFileID() const
Returns the FileID of the main source file.
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
Reads an AST files chain containing the contents of a translation unit.
FileManager & getFileManager() const
Return the current file manager to the caller.
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getBufferForFile(const FileEntry *Entry, bool isVolatile=false, bool ShouldCloseOpenFile=true)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
virtual std::unique_ptr< ASTConsumer > CreatePCHContainerGenerator(CompilerInstance &CI, const std::string &MainFileName, const std::string &OutputFileName, std::unique_ptr< llvm::raw_pwrite_stream > OS, std::shared_ptr< PCHBuffer > Buffer) const =0
Return an ASTConsumer that can be chained with a PCHGenerator that produces a wrapper file format con...
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
Abstract interface for callback invocations by the ASTReader.
std::unique_ptr< ASTConsumer > CreateDeclContextPrinter()
~SyntaxOnlyAction() override
std::vector< std::shared_ptr< ModuleFileExtension > > ModuleFileExtensions
The list of module file extensions.
std::unique_ptr< raw_pwrite_stream > createDefaultOutputFile(bool Binary=true, StringRef BaseInput="", StringRef Extension="")
Create the default output file (from the invocation's options) and add it to the list of tracked outp...
std::unique_ptr< ASTConsumer > CreateASTViewer()
const PCHContainerWriter & getPCHContainerWriter() const
Return the appropriate PCHContainerWriter depending on the current CodeGenOptions.
std::unique_ptr< ASTConsumer > CreateASTDeclNodeLister()
virtual bool BeginSourceFileAction(CompilerInstance &CI)
Callback at the start of processing a single input.
unsigned ASTDumpDecls
Whether we include declaration dumps in AST dumps.
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
std::pair< unsigned, bool > PrecompiledPreambleBytes
If non-zero, the implicit PCH include is actually a precompiled preamble that covers this number of b...
static bool readASTFileControlBlock(StringRef Filename, FileManager &FileMgr, const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions, ASTReaderListener &Listener, bool ValidateDiagnosticOptions)
Read the control block for the named AST file.
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
std::unique_ptr< ASTConsumer > CreateASTPrinter(std::unique_ptr< raw_ostream > OS, StringRef FilterString)
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
void SetKeepWhitespaceMode(bool Val)
SetKeepWhitespaceMode - This method lets clients enable or disable whitespace retention mode...
std::string Triple
The name of the target triple to compile for.
This class handles loading and caching of source files into memory.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer...