25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/raw_ostream.h"
29 #include <system_error>
31 using namespace clang;
37 std::unique_ptr<ASTConsumer>
39 return llvm::make_unique<ASTConsumer>();
42 void InitOnlyAction::ExecuteAction() {
49 std::unique_ptr<ASTConsumer>
56 std::unique_ptr<ASTConsumer>
63 std::unique_ptr<ASTConsumer>
68 std::unique_ptr<ASTConsumer>
73 std::unique_ptr<ASTConsumer>
79 std::unique_ptr<ASTConsumer>
82 std::string OutputFile;
83 raw_pwrite_stream *OS =
91 auto Buffer = std::make_shared<PCHBuffer>();
92 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
93 Consumers.push_back(llvm::make_unique<PCHGenerator>(
99 InFile, OutputFile, OS, Buffer));
101 return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
106 std::string &OutputFile) {
116 raw_pwrite_stream *OS =
127 std::unique_ptr<ASTConsumer>
131 std::string OutputFile;
132 raw_pwrite_stream *OS =
137 auto Buffer = std::make_shared<PCHBuffer>();
138 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
139 Consumers.push_back(llvm::make_unique<PCHGenerator>(
145 InFile, OutputFile, OS, Buffer));
146 return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
151 Includes.append(RHS.begin(), RHS.end());
156 SmallVectorImpl<char> &Includes,
159 if (IsExternC && LangOpts.CPlusPlus)
160 Includes +=
"extern \"C\" {\n";
162 Includes +=
"#import \"";
164 Includes +=
"#include \"";
166 Includes += HeaderName;
169 if (IsExternC && LangOpts.CPlusPlus)
171 return std::error_code();
181 static std::error_code
184 SmallVectorImpl<char> &Includes) {
187 return std::error_code();
215 llvm::sys::path::native(UmbrellaDir.Entry->getName(), DirNative);
216 for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative, EC),
218 Dir != DirEnd && !EC; Dir.increment(EC)) {
221 if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
222 .Cases(
".h",
".H",
".hh",
".hpp",
true)
239 auto PathIt = llvm::sys::path::rbegin(Dir->path());
240 for (
int I = 0; I != Dir.level() + 1; ++I, ++PathIt)
241 Components.push_back(*PathIt);
243 for (
auto It = Components.rbegin(),
End = Components.rend(); It !=
End;
245 llvm::sys::path::append(RelativeHeader, *It);
261 Sub != SubEnd; ++Sub)
263 LangOpts, FileMgr, ModMap, *Sub, Includes))
266 return std::error_code();
270 StringRef Filename) {
319 diag::err_module_header_missing)
324 << Requirement.second << Requirement.first;
330 if (ModuleMapForUniquing && ModuleMapForUniquing != ModuleMap) {
334 ModuleMapForUniquing = ModuleMap;
341 std::error_code Err = std::error_code();
361 std::unique_ptr<llvm::MemoryBuffer> InputBuffer =
362 llvm::MemoryBuffer::getMemBufferCopy(HeaderContents,
372 std::string &OutputFile) {
379 ModuleMapForUniquing->
getName());
385 raw_pwrite_stream *OS =
397 std::unique_ptr<ASTConsumer>
399 return llvm::make_unique<ASTConsumer>();
402 std::unique_ptr<ASTConsumer>
405 return llvm::make_unique<ASTConsumer>();
408 std::unique_ptr<ASTConsumer>
410 return llvm::make_unique<ASTConsumer>();
417 std::unique_ptr<ASTReader> Reader(
new ASTReader(
419 Sysroot.empty() ?
"" : Sysroot.c_str(),
436 llvm::raw_ostream &Out;
439 DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { }
441 #define DUMP_BOOLEAN(Value, Text) \
442 Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n"
444 bool ReadFullVersionInformation(StringRef FullVersion)
override {
449 <<
" Clang: " << FullVersion <<
"\n";
453 void ReadModuleName(StringRef ModuleName)
override {
454 Out.indent(2) <<
"Module name: " << ModuleName <<
"\n";
456 void ReadModuleMapFile(StringRef ModuleMapPath)
override {
457 Out.indent(2) <<
"Module map file: " << ModuleMapPath <<
"\n";
460 bool ReadLanguageOptions(
const LangOptions &LangOpts,
bool Complain,
461 bool AllowCompatibleDifferences)
override {
462 Out.indent(2) <<
"Language options:\n";
463 #define LANGOPT(Name, Bits, Default, Description) \
464 DUMP_BOOLEAN(LangOpts.Name, Description);
465 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
466 Out.indent(4) << Description << ": " \
467 << static_cast<unsigned>(LangOpts.get##Name()) << "\n";
468 #define VALUE_LANGOPT(Name, Bits, Default, Description) \
469 Out.indent(4) << Description << ": " << LangOpts.Name << "\n";
470 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
471 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
472 #include "clang/Basic/LangOptions.def"
475 Out.indent(4) <<
"Module features:\n";
477 Out.indent(6) << Feature <<
"\n";
483 bool ReadTargetOptions(
const TargetOptions &TargetOpts,
bool Complain,
484 bool AllowCompatibleDifferences)
override {
485 Out.indent(2) <<
"Target options:\n";
486 Out.indent(4) <<
" Triple: " << TargetOpts.
Triple <<
"\n";
487 Out.indent(4) <<
" CPU: " << TargetOpts.
CPU <<
"\n";
488 Out.indent(4) <<
" ABI: " << TargetOpts.
ABI <<
"\n";
491 Out.indent(4) <<
"Target features:\n";
502 bool Complain)
override {
503 Out.indent(2) <<
"Diagnostic options:\n";
504 #define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name);
505 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
506 Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n";
507 #define VALUE_DIAGOPT(Name, Bits, Default) \
508 Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n";
509 #include "clang/Basic/DiagnosticOptions.def"
511 Out.indent(4) <<
"Diagnostic flags:\n";
512 for (
const std::string &
Warning : DiagOpts->Warnings)
513 Out.indent(6) <<
"-W" << Warning <<
"\n";
514 for (
const std::string &
Remark : DiagOpts->Remarks)
515 Out.indent(6) <<
"-R" << Remark <<
"\n";
521 StringRef SpecificModuleCachePath,
522 bool Complain)
override {
523 Out.indent(2) <<
"Header search options:\n";
524 Out.indent(4) <<
"System root [-isysroot=]: '" << HSOpts.
Sysroot <<
"'\n";
525 Out.indent(4) <<
"Module Cache: '" << SpecificModuleCachePath <<
"'\n";
527 "Use builtin include directories [-nobuiltininc]");
529 "Use standard system include directories [-nostdinc]");
531 "Use standard C++ include directories [-nostdinc++]");
533 "Use libc++ (rather than libstdc++) [-stdlib=]");
539 std::string &SuggestedPredefines)
override {
540 Out.indent(2) <<
"Preprocessor options:\n";
542 "Uses compiler/target-specific predefines [-undef]");
544 "Uses detailed preprocessing record (for indexing)");
546 if (!PPOpts.
Macros.empty()) {
547 Out.indent(4) <<
"Predefined macros:\n";
550 for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
558 Out << I->first <<
"\n";
568 std::unique_ptr<llvm::raw_fd_ostream> OutFile;
570 if (!OutputFileName.empty() && OutputFileName !=
"-") {
572 OutFile.reset(
new llvm::raw_fd_ostream(OutputFileName.str(), EC,
573 llvm::sys::fs::F_Text));
575 llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs();
577 Out <<
"Information for module file '" <<
getCurrentFile() <<
"':\n";
578 DumpModuleInfoListener Listener(Out);
598 RawLex.LexFromRawLexer(RawTok);
601 llvm::errs() <<
"\n";
602 RawLex.LexFromRawLexer(RawTok);
614 llvm::errs() <<
"\n";
656 bool BinaryMode =
true;
657 bool InvalidFile =
false;
662 const char *cur = Buffer->getBufferStart();
663 const char *end = Buffer->getBufferEnd();
664 const char *next = (cur != end) ? cur + 1 : end;
669 if (end - cur > 256) end = cur + 256;
677 }
else if (*cur == 0x0A)
719 llvm::outs().write((*Buffer)->getBufferStart(), Preamble);
std::string OutputFile
The output file, if any.
SourceManager & getSourceManager() const
LangOptions & getLangOpts()
ASTContext & getASTContext() const
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
PreprocessorOptions & getPreprocessorOpts()
Header getUmbrellaHeader() const
Retrieve the header that serves as the umbrella header for this module.
std::vector< std::pair< std::string, bool > > Macros
bool BeginSourceFileAction(CompilerInstance &CI, StringRef Filename) override
Callback at the start of processing a single input.
Implements support for file system lookup, file system caching, and directory search management...
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.
submodule_iterator submodule_begin()
unsigned IsExternC
Whether this is an 'extern "C"' module (which implicitly puts all headers in it within an 'extern "...
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.
static bool readASTFileControlBlock(StringRef Filename, FileManager &FileMgr, const PCHContainerReader &PCHContainerRdr, ASTReaderListener &Listener)
Read the control block for the named AST file.
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
std::string ASTDumpFilter
If given, filter dumped AST Decl nodes by this substring.
TargetInfo & getTarget() const
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
bool isHeaderUnavailableInModule(const FileEntry *Header, const Module *RequestingModule) const
Determine whether the given header is unavailable as part of the specified module.
void DumpToken(const Token &Tok, bool DumpFlags=false) const
Print the token to stderr, used for debugging.
std::string getFullModuleName() const
Retrieve the full name of this module, including the path from its top-level module.
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
static StringRef getModuleInputBufferName()
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
const LangOptions & getLangOpts() const
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.
Describes a module or submodule.
bool isAvailable() const
Determine whether this module is available for use within the current translation unit...
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 * Parent
The parent of this module. This will be NULL for the top-level module.
unsigned IsInferred
Whether this is an inferred submodule (module * { ... }).
submodule_iterator submodule_end()
void CacheTokens(Preprocessor &PP, raw_pwrite_stream *OS)
Cache tokens for use with PCH. Note that this requires a seekable stream.
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...
Preprocessor & getPreprocessor() const
Return the current preprocessor.
std::vector< Module * >::iterator submodule_iterator
std::vector< std::string > ModuleFeatures
The names of any features to enable in module 'requires' decls in addition to the hard-coded list in ...
unsigned IsSystem
Whether this is a "system" module (which assumes that all headers in it are system headers)...
const FileEntry * getFile(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Lookup, cache, and verify the specified file (real or virtual).
std::string CurrentModule
The name of the current module.
static std::error_code addHeaderInclude(StringRef HeaderName, SmallVectorImpl< char > &Includes, const LangOptions &LangOpts, bool IsExternC)
void setCurrentInput(const FrontendInputFile &CurrentInput, std::unique_ptr< ASTUnit > AST=nullptr)
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.
ModuleBuildStack getModuleBuildStack() const
Retrieve the module build stack.
static SmallVectorImpl< char > & operator+=(SmallVectorImpl< char > &Includes, StringRef RHS)
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.
DirectoryName getUmbrellaDir() const
Retrieve the directory for which this module serves as the umbrella.
static std::error_code collectModuleHeaderIncludes(const LangOptions &LangOpts, FileManager &FileMgr, ModuleMap &ModMap, clang::Module *Module, SmallVectorImpl< char > &Includes)
Collect the set of header includes needed to construct the given module and update the TopHeaders fil...
std::string CPU
If given, the name of the target CPU to generate code for.
const DirectoryEntry * Directory
The build directory of this module. This is the directory in which the module is notionally built...
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.
const char * getName() const
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
Encodes a location in the source. The SourceManager can decode this to get at the full include stack...
const PCHContainerReader & getPCHContainerReader() const
File is a PCH file treated as such.
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 ...
bool isValid() const
Return true if this is a valid SourceLocation object.
const StringRef getCurrentFile() const
Information about a directory name as found in the module map file.
Cached information about one file (either on disk or in the virtual file system). ...
SmallVector< Header, 2 > Headers[5]
The headers that are part of this module.
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.
std::vector< std::string > FeaturesAsWritten
The list of target specific features to enable or disable, as written on the command line...
void pushModuleBuildStack(StringRef moduleName, FullSourceLoc importLoc)
Push an entry to the module build stack.
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.
std::unique_ptr< ASTConsumer > CreateASTPrinter(raw_ostream *OS, StringRef FilterString)
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.
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
void addTopHeader(const FileEntry *File)
Add a top-level header associated with this module.
Abstract interface for callback invocations by the ASTReader.
std::unique_ptr< ASTConsumer > CreateDeclContextPrinter()
std::pair< std::string, bool > Requirement
An individual requirement: a feature name and a flag indicating the required state of that feature...
std::unique_ptr< ASTConsumer > CreateASTViewer()
const PCHContainerWriter & getPCHContainerWriter() const
raw_pwrite_stream * createDefaultOutputFile(bool Binary=true, StringRef BaseInput="", StringRef Extension="")
std::unique_ptr< ASTConsumer > CreateASTDeclNodeLister()
void setMainFileDir(const DirectoryEntry *Dir)
Set the directory in which the main file should be considered to have been found, if it is not a real...
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...
A SourceLocation and its associated SourceManager.
virtual std::unique_ptr< ASTConsumer > CreatePCHContainerGenerator(DiagnosticsEngine &Diags, const HeaderSearchOptions &HSO, const PreprocessorOptions &PPO, const TargetOptions &TO, const LangOptions &LO, const std::string &MainFileName, const std::string &OutputFileName, llvm::raw_pwrite_stream *OS, std::shared_ptr< PCHBuffer > Buffer) const =0
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
static 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 ...
std::unique_ptr< ASTConsumer > CreateASTDumper(StringRef FilterString, bool DumpDecls, bool DumpLookups)
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
void SetKeepWhitespaceMode(bool Val)
raw_pwrite_stream * createOutputFile(StringRef OutputPath, bool Binary, bool RemoveFileOnSignal, StringRef BaseInput, StringRef Extension, bool UseTemporary, bool CreateMissingDirectories=false)
void setInferredModuleAllowedBy(Module *M, const FileEntry *ModuleMap)
TargetOptions & getTargetOpts()
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...