#include "clang/Frontend/FrontendAction.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclGroup.h"
#include "clang/Basic/Builtins.h"
#include "clang/Basic/LangStandard.h"
#include "clang/Frontend/ASTUnit.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendDiagnostic.h"
#include "clang/Frontend/FrontendPluginRegistry.h"
#include "clang/Frontend/LayoutOverrideSource.h"
#include "clang/Frontend/MultiplexConsumer.h"
#include "clang/Frontend/Utils.h"
#include "clang/Lex/HeaderSearch.h"
#include "clang/Lex/LiteralSupport.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/PreprocessorOptions.h"
#include "clang/Parse/ParseAST.h"
#include "clang/Sema/HLSLExternalSemaSource.h"
#include "clang/Serialization/ASTDeserializationListener.h"
#include "clang/Serialization/ASTReader.h"
#include "clang/Serialization/GlobalModuleIndex.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/Support/BuryPointer.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Timer.h"
#include "llvm/Support/raw_ostream.h"
#include <system_error>
using namespace clang;
LLVM_INSTANTIATE_REGISTRY(FrontendPluginRegistry)
namespace {
class DelegatingDeserializationListener : public ASTDeserializationListener {
ASTDeserializationListener *Previous;
bool DeletePrevious;
public:
explicit DelegatingDeserializationListener(
ASTDeserializationListener *Previous, bool DeletePrevious)
: Previous(Previous), DeletePrevious(DeletePrevious) {}
~DelegatingDeserializationListener() override {
if (DeletePrevious)
delete Previous;
}
void ReaderInitialized(ASTReader *Reader) override {
if (Previous)
Previous->ReaderInitialized(Reader);
}
void IdentifierRead(serialization::IdentID ID,
IdentifierInfo *II) override {
if (Previous)
Previous->IdentifierRead(ID, II);
}
void TypeRead(serialization::TypeIdx Idx, QualType T) override {
if (Previous)
Previous->TypeRead(Idx, T);
}
void DeclRead(serialization::DeclID ID, const Decl *D) override {
if (Previous)
Previous->DeclRead(ID, D);
}
void SelectorRead(serialization::SelectorID ID, Selector Sel) override {
if (Previous)
Previous->SelectorRead(ID, Sel);
}
void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
MacroDefinitionRecord *MD) override {
if (Previous)
Previous->MacroDefinitionRead(PPID, MD);
}
};
class DeserializedDeclsDumper : public DelegatingDeserializationListener {
public:
explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous,
bool DeletePrevious)
: DelegatingDeserializationListener(Previous, DeletePrevious) {}
void DeclRead(serialization::DeclID ID, const Decl *D) override {
llvm::outs() << "PCH DECL: " << D->getDeclKindName();
if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
llvm::outs() << " - ";
ND->printQualifiedName(llvm::outs());
}
llvm::outs() << "\n";
DelegatingDeserializationListener::DeclRead(ID, D);
}
};
class DeserializedDeclsChecker : public DelegatingDeserializationListener {
ASTContext &Ctx;
std::set<std::string> NamesToCheck;
public:
DeserializedDeclsChecker(ASTContext &Ctx,
const std::set<std::string> &NamesToCheck,
ASTDeserializationListener *Previous,
bool DeletePrevious)
: DelegatingDeserializationListener(Previous, DeletePrevious), Ctx(Ctx),
NamesToCheck(NamesToCheck) {}
void DeclRead(serialization::DeclID ID, const Decl *D) override {
if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) {
unsigned DiagID
= Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error,
"%0 was deserialized");
Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID)
<< ND;
}
DelegatingDeserializationListener::DeclRead(ID, D);
}
};
}
FrontendAction::FrontendAction() : Instance(nullptr) {}
FrontendAction::~FrontendAction() {}
void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
std::unique_ptr<ASTUnit> AST) {
this->CurrentInput = CurrentInput;
CurrentASTUnit = std::move(AST);
}
Module *FrontendAction::getCurrentModule() const {
CompilerInstance &CI = getCompilerInstance();
return CI.getPreprocessor().getHeaderSearchInfo().lookupModule(
CI.getLangOpts().CurrentModule, SourceLocation(), false);
}
std::unique_ptr<ASTConsumer>
FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
StringRef InFile) {
std::unique_ptr<ASTConsumer> Consumer = CreateASTConsumer(CI, InFile);
if (!Consumer)
return nullptr;
bool FoundAllPlugins = true;
for (const std::string &Arg : CI.getFrontendOpts().AddPluginActions) {
bool Found = false;
for (const FrontendPluginRegistry::entry &Plugin :
FrontendPluginRegistry::entries()) {
if (Plugin.getName() == Arg)
Found = true;
}
if (!Found) {
CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name) << Arg;
FoundAllPlugins = false;
}
}
if (!FoundAllPlugins)
return nullptr;
if (FrontendPluginRegistry::begin() == FrontendPluginRegistry::end())
return Consumer;
if (CI.hasCodeCompletionConsumer())
return Consumer;
std::vector<std::unique_ptr<ASTConsumer>> Consumers;
std::vector<std::unique_ptr<ASTConsumer>> AfterConsumers;
for (const FrontendPluginRegistry::entry &Plugin :
FrontendPluginRegistry::entries()) {
std::unique_ptr<PluginASTAction> P = Plugin.instantiate();
PluginASTAction::ActionType ActionType = P->getActionType();
if (ActionType == PluginASTAction::CmdlineAfterMainAction ||
ActionType == PluginASTAction::CmdlineBeforeMainAction) {
if (llvm::any_of(CI.getFrontendOpts().AddPluginActions,
[&](const std::string &PluginAction) {
return PluginAction == Plugin.getName();
})) {
if (ActionType == PluginASTAction::CmdlineBeforeMainAction)
ActionType = PluginASTAction::AddBeforeMainAction;
else
ActionType = PluginASTAction::AddAfterMainAction;
}
}
if ((ActionType == PluginASTAction::AddBeforeMainAction ||
ActionType == PluginASTAction::AddAfterMainAction) &&
P->ParseArgs(
CI,
CI.getFrontendOpts().PluginArgs[std::string(Plugin.getName())])) {
std::unique_ptr<ASTConsumer> PluginConsumer = P->CreateASTConsumer(CI, InFile);
if (ActionType == PluginASTAction::AddBeforeMainAction) {
Consumers.push_back(std::move(PluginConsumer));
} else {
AfterConsumers.push_back(std::move(PluginConsumer));
}
}
}
Consumers.push_back(std::move(Consumer));
if (!AfterConsumers.empty()) {
CI.getCodeGenOpts().ClearASTBeforeBackend = false;
for (auto &C : AfterConsumers)
Consumers.push_back(std::move(C));
}
return std::make_unique<MultiplexConsumer>(std::move(Consumers));
}
static SourceLocation ReadOriginalFileName(CompilerInstance &CI,
std::string &InputFile,
bool IsModuleMap = false) {
auto &SourceMgr = CI.getSourceManager();
auto MainFileID = SourceMgr.getMainFileID();
auto MainFileBuf = SourceMgr.getBufferOrNone(MainFileID);
if (!MainFileBuf)
return SourceLocation();
std::unique_ptr<Lexer> RawLexer(
new Lexer(MainFileID, *MainFileBuf, SourceMgr, CI.getLangOpts()));
Token T;
if (RawLexer->LexFromRawLexer(T) || T.getKind() != tok::hash)
return SourceLocation();
if (RawLexer->LexFromRawLexer(T) || T.isAtStartOfLine() ||
T.getKind() != tok::numeric_constant)
return SourceLocation();
unsigned LineNo;
SourceLocation LineNoLoc = T.getLocation();
if (IsModuleMap) {
llvm::SmallString<16> Buffer;
if (Lexer::getSpelling(LineNoLoc, Buffer, SourceMgr, CI.getLangOpts())
.getAsInteger(10, LineNo))
return SourceLocation();
}
RawLexer->LexFromRawLexer(T);
if (T.isAtStartOfLine() || T.getKind() != tok::string_literal)
return SourceLocation();
StringLiteralParser Literal(T, CI.getPreprocessor());
if (Literal.hadError)
return SourceLocation();
RawLexer->LexFromRawLexer(T);
if (T.isNot(tok::eof) && !T.isAtStartOfLine())
return SourceLocation();
InputFile = Literal.GetString().str();
if (IsModuleMap)
CI.getSourceManager().AddLineNote(
LineNoLoc, LineNo, SourceMgr.getLineTableFilenameID(InputFile), false,
false, SrcMgr::C_User_ModuleMap);
return T.getLocation();
}
static SmallVectorImpl<char> &
operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) {
Includes.append(RHS.begin(), RHS.end());
return Includes;
}
static void addHeaderInclude(StringRef HeaderName,
SmallVectorImpl<char> &Includes,
const LangOptions &LangOpts,
bool IsExternC) {
if (IsExternC && LangOpts.CPlusPlus)
Includes += "extern \"C\" {\n";
if (LangOpts.ObjC)
Includes += "#import \"";
else
Includes += "#include \"";
Includes += HeaderName;
Includes += "\"\n";
if (IsExternC && LangOpts.CPlusPlus)
Includes += "}\n";
}
static std::error_code collectModuleHeaderIncludes(
const LangOptions &LangOpts, FileManager &FileMgr, DiagnosticsEngine &Diag,
ModuleMap &ModMap, clang::Module *Module, SmallVectorImpl<char> &Includes) {
if (!Module->isAvailable())
return std::error_code();
ModMap.resolveHeaderDirectives(Module, llvm::None);
if (!Module->MissingHeaders.empty()) {
auto &MissingHeader = Module->MissingHeaders.front();
Diag.Report(MissingHeader.FileNameLoc, diag::err_module_header_missing)
<< MissingHeader.IsUmbrella << MissingHeader.FileName;
return std::error_code();
}
for (auto HK : {Module::HK_Normal, Module::HK_Private}) {
for (Module::Header &H : Module->Headers[HK]) {
Module->addTopHeader(H.Entry);
addHeaderInclude(H.PathRelativeToRootModuleDirectory, Includes, LangOpts,
Module->IsExternC);
}
}
if (Module::Header UmbrellaHeader = Module->getUmbrellaHeader()) {
Module->addTopHeader(UmbrellaHeader.Entry);
if (Module->Parent)
addHeaderInclude(UmbrellaHeader.PathRelativeToRootModuleDirectory,
Includes, LangOpts, Module->IsExternC);
} else if (Module::DirectoryName UmbrellaDir = Module->getUmbrellaDir()) {
std::error_code EC;
SmallString<128> DirNative;
llvm::sys::path::native(UmbrellaDir.Entry->getName(), DirNative);
llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
SmallVector<std::pair<std::string, const FileEntry *>, 8> Headers;
for (llvm::vfs::recursive_directory_iterator Dir(FS, DirNative, EC), End;
Dir != End && !EC; Dir.increment(EC)) {
if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
.Cases(".h", ".H", ".hh", ".hpp", true)
.Default(false))
continue;
auto Header = FileMgr.getFile(Dir->path());
if (!Header)
continue;
if (ModMap.isHeaderUnavailableInModule(*Header, Module))
continue;
SmallVector<StringRef, 16> Components;
auto PathIt = llvm::sys::path::rbegin(Dir->path());
for (int I = 0; I != Dir.level() + 1; ++I, ++PathIt)
Components.push_back(*PathIt);
SmallString<128> RelativeHeader(
UmbrellaDir.PathRelativeToRootModuleDirectory);
for (auto It = Components.rbegin(), End = Components.rend(); It != End;
++It)
llvm::sys::path::append(RelativeHeader, *It);
std::string RelName = RelativeHeader.c_str();
Headers.push_back(std::make_pair(RelName, *Header));
}
if (EC)
return EC;
llvm::sort(Headers, llvm::less_first());
for (auto &H : Headers) {
Module->addTopHeader(H.second);
addHeaderInclude(H.first, Includes, LangOpts, Module->IsExternC);
}
}
for (clang::Module::submodule_iterator Sub = Module->submodule_begin(),
SubEnd = Module->submodule_end();
Sub != SubEnd; ++Sub)
if (std::error_code Err = collectModuleHeaderIncludes(
LangOpts, FileMgr, Diag, ModMap, *Sub, Includes))
return Err;
return std::error_code();
}
static bool loadModuleMapForModuleBuild(CompilerInstance &CI, bool IsSystem,
bool IsPreprocessed,
std::string &PresumedModuleMapFile,
unsigned &Offset) {
auto &SrcMgr = CI.getSourceManager();
HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
FileID ModuleMapID = SrcMgr.getMainFileID();
const FileEntry *ModuleMap = SrcMgr.getFileEntryForID(ModuleMapID);
Offset = 0;
if (IsPreprocessed) {
SourceLocation EndOfLineMarker =
ReadOriginalFileName(CI, PresumedModuleMapFile, true);
if (EndOfLineMarker.isValid())
Offset = CI.getSourceManager().getDecomposedLoc(EndOfLineMarker).second;
}
if (HS.loadModuleMapFile(ModuleMap, IsSystem, ModuleMapID, &Offset,
PresumedModuleMapFile))
return true;
if (SrcMgr.getBufferOrFake(ModuleMapID).getBufferSize() == Offset)
Offset = 0;
if (HS.getModuleMap().canInferFrameworkModule(ModuleMap->getDir())) {
SmallString<128> InferredFrameworkPath = ModuleMap->getDir()->getName();
llvm::sys::path::append(InferredFrameworkPath,
CI.getLangOpts().ModuleName + ".framework");
if (auto Dir = CI.getFileManager().getDirectory(InferredFrameworkPath))
(void)HS.getModuleMap().inferFrameworkModule(*Dir, IsSystem, nullptr);
}
return false;
}
static Module *prepareToBuildModule(CompilerInstance &CI,
StringRef ModuleMapFilename) {
if (CI.getLangOpts().CurrentModule.empty()) {
CI.getDiagnostics().Report(diag::err_missing_module_name);
return nullptr;
}
HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
Module *M = HS.lookupModule(CI.getLangOpts().CurrentModule, SourceLocation(),
true);
if (!M) {
CI.getDiagnostics().Report(diag::err_missing_module)
<< CI.getLangOpts().CurrentModule << ModuleMapFilename;
return nullptr;
}
if (Preprocessor::checkModuleIsAvailable(CI.getLangOpts(), CI.getTarget(),
CI.getDiagnostics(), M))
return nullptr;
CI.getPreprocessor().setMainFileDir(M->Directory);
StringRef OriginalModuleMapName = CI.getFrontendOpts().OriginalModuleMap;
if (!OriginalModuleMapName.empty()) {
auto OriginalModuleMap =
CI.getFileManager().getFile(OriginalModuleMapName,
true);
if (!OriginalModuleMap) {
CI.getDiagnostics().Report(diag::err_module_map_not_found)
<< OriginalModuleMapName;
return nullptr;
}
if (*OriginalModuleMap != CI.getSourceManager().getFileEntryForID(
CI.getSourceManager().getMainFileID())) {
M->IsInferred = true;
CI.getPreprocessor().getHeaderSearchInfo().getModuleMap()
.setInferredModuleAllowedBy(M, *OriginalModuleMap);
}
}
SourceManager &SourceMgr = CI.getSourceManager();
if (SourceMgr.getModuleBuildStack().empty())
SourceMgr.pushModuleBuildStack(CI.getLangOpts().CurrentModule,
FullSourceLoc(SourceLocation(), SourceMgr));
return M;
}
static std::unique_ptr<llvm::MemoryBuffer>
getInputBufferForModule(CompilerInstance &CI, Module *M) {
FileManager &FileMgr = CI.getFileManager();
SmallString<256> HeaderContents;
std::error_code Err = std::error_code();
if (Module::Header UmbrellaHeader = M->getUmbrellaHeader())
addHeaderInclude(UmbrellaHeader.PathRelativeToRootModuleDirectory,
HeaderContents, CI.getLangOpts(), M->IsExternC);
Err = collectModuleHeaderIncludes(
CI.getLangOpts(), FileMgr, CI.getDiagnostics(),
CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(), M,
HeaderContents);
if (Err) {
CI.getDiagnostics().Report(diag::err_module_cannot_create_includes)
<< M->getFullModuleName() << Err.message();
return nullptr;
}
return llvm::MemoryBuffer::getMemBufferCopy(
HeaderContents, Module::getModuleInputBufferName());
}
bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
const FrontendInputFile &RealInput) {
FrontendInputFile Input(RealInput);
assert(!Instance && "Already processing a source file!");
assert(!Input.isEmpty() && "Unexpected empty filename!");
setCurrentInput(Input);
setCompilerInstance(&CI);
bool HasBegunSourceFile = false;
bool ReplayASTFile = Input.getKind().getFormat() == InputKind::Precompiled &&
usesPreprocessorOnly();
auto FailureCleanup = llvm::make_scope_exit([&]() {
if (HasBegunSourceFile)
CI.getDiagnosticClient().EndSourceFile();
CI.setASTConsumer(nullptr);
CI.clearOutputFiles(true);
CI.getLangOpts().setCompilingModule(LangOptions::CMK_None);
setCurrentInput(FrontendInputFile());
setCompilerInstance(nullptr);
});
if (!BeginInvocation(CI))
return false;
if (ReplayASTFile) {
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
IntrusiveRefCntPtr<DiagnosticsEngine> ASTDiags(
new DiagnosticsEngine(Diags->getDiagnosticIDs(),
&Diags->getDiagnosticOptions()));
ASTDiags->setClient(Diags->getClient(), false);
StringRef InputFile = Input.getFile();
std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
std::string(InputFile), CI.getPCHContainerReader(),
ASTUnit::LoadPreprocessorOnly, ASTDiags, CI.getFileSystemOpts(),
CI.getCodeGenOpts().DebugTypeExtRefs);
if (!AST)
return false;
CI.getHeaderSearchOpts() = AST->getHeaderSearchOpts();
CI.getPreprocessorOpts() = AST->getPreprocessorOpts();
CI.getLangOpts() = AST->getLangOpts();
CI.setFileManager(&AST->getFileManager());
CI.createSourceManager(CI.getFileManager());
CI.getSourceManager().initializeForReplay(AST->getSourceManager());
if (auto ASTReader = AST->getASTReader()) {
auto &MM = ASTReader->getModuleManager();
auto &PrimaryModule = MM.getPrimaryModule();
for (serialization::ModuleFile &MF : MM)
if (&MF != &PrimaryModule)
CI.getFrontendOpts().ModuleFiles.push_back(MF.FileName);
ASTReader->visitTopLevelModuleMaps(
PrimaryModule, [&](const FileEntry *FE) {
CI.getFrontendOpts().ModuleMapFiles.push_back(
std::string(FE->getName()));
});
}
auto Kind = AST->getInputKind();
if (Kind.getFormat() == InputKind::ModuleMap) {
Module *ASTModule =
AST->getPreprocessor().getHeaderSearchInfo().lookupModule(
AST->getLangOpts().CurrentModule, SourceLocation(),
false);
assert(ASTModule && "module file does not define its own module");
Input = FrontendInputFile(ASTModule->PresumedModuleMapFile, Kind);
} else {
auto &OldSM = AST->getSourceManager();
FileID ID = OldSM.getMainFileID();
if (auto *File = OldSM.getFileEntryForID(ID))
Input = FrontendInputFile(File->getName(), Kind);
else
Input = FrontendInputFile(OldSM.getBufferOrFake(ID), Kind);
}
setCurrentInput(Input, std::move(AST));
}
if (Input.getKind().getFormat() == InputKind::Precompiled) {
assert(!usesPreprocessorOnly() && "this case was handled above");
assert(hasASTFileSupport() &&
"This action does not have AST file support!");
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
StringRef InputFile = Input.getFile();
std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
std::string(InputFile), CI.getPCHContainerReader(),
ASTUnit::LoadEverything, Diags, CI.getFileSystemOpts(),
CI.getCodeGenOpts().DebugTypeExtRefs);
if (!AST)
return false;
CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
HasBegunSourceFile = true;
CI.setFileManager(&AST->getFileManager());
CI.setSourceManager(&AST->getSourceManager());
CI.setPreprocessor(AST->getPreprocessorPtr());
Preprocessor &PP = CI.getPreprocessor();
PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
PP.getLangOpts());
CI.setASTContext(&AST->getASTContext());
setCurrentInput(Input, std::move(AST));
if (!BeginSourceFileAction(CI))
return false;
CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
if (!CI.hasASTConsumer())
return false;
FailureCleanup.release();
return true;
}
if (!CI.hasFileManager()) {
if (!CI.createFileManager()) {
return false;
}
}
if (!CI.hasSourceManager())
CI.createSourceManager(CI.getFileManager());
for (const auto &F : CI.getFrontendOpts().ModulesEmbedFiles) {
if (auto FE = CI.getFileManager().getFile(F, true))
CI.getSourceManager().setFileIsTransient(*FE);
else
CI.getDiagnostics().Report(diag::err_modules_embed_file_not_found) << F;
}
if (CI.getFrontendOpts().ModulesEmbedAllFiles)
CI.getSourceManager().setAllFilesAreTransient(true);
if (Input.getKind().getLanguage() == Language::LLVM_IR) {
assert(hasIRSupport() &&
"This action does not have IR file support!");
CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
HasBegunSourceFile = true;
if (!BeginSourceFileAction(CI))
return false;
if (!CI.InitializeSourceManager(CurrentInput))
return false;
FailureCleanup.release();
return true;
}
if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
FileManager &FileMgr = CI.getFileManager();
PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
std::string SpecificModuleCachePath = CI.getSpecificModuleCachePath();
if (auto PCHDir = FileMgr.getOptionalDirectoryRef(PCHInclude)) {
std::error_code EC;
SmallString<128> DirNative;
llvm::sys::path::native(PCHDir->getName(), DirNative);
bool Found = false;
llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC),
DirEnd;
Dir != DirEnd && !EC; Dir.increment(EC)) {
if (ASTReader::isAcceptableASTFile(
Dir->path(), FileMgr, CI.getPCHContainerReader(),
CI.getLangOpts(), CI.getTargetOpts(), CI.getPreprocessorOpts(),
SpecificModuleCachePath, true)) {
PPOpts.ImplicitPCHInclude = std::string(Dir->path());
Found = true;
break;
}
}
if (!Found) {
CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
return false;
}
}
}
if (!isModelParsingAction())
CI.createPreprocessor(getTranslationUnitKind());
CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
&CI.getPreprocessor());
HasBegunSourceFile = true;
if (CI.getLangOpts().CPlusPlusModules && Input.getKind().isHeaderUnit() &&
!Input.getKind().isPreprocessed()) {
StringRef FileName = Input.getFile();
InputKind Kind = Input.getKind();
if (Kind.getHeaderUnitKind() != InputKind::HeaderUnit_Abs) {
assert(CI.hasPreprocessor() &&
"trying to build a header unit without a Pre-processor?");
HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
const DirectoryEntry *Dir = nullptr;
if (auto DirOrErr = CI.getFileManager().getDirectory("."))
Dir = *DirOrErr;
SmallVector<std::pair<const FileEntry *, const DirectoryEntry *>, 1> CWD;
CWD.push_back({nullptr, Dir});
Optional<FileEntryRef> FE =
HS.LookupFile(FileName, SourceLocation(),
Input.getKind().getHeaderUnitKind() ==
InputKind::HeaderUnit_System,
nullptr, nullptr, CWD, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr);
if (!FE) {
CI.getDiagnostics().Report(diag::err_module_header_file_not_found)
<< FileName;
return false;
}
FileName = FE->getFileEntry().getName();
Kind = Input.getKind().withHeaderUnit(InputKind::HeaderUnit_Abs);
Input = FrontendInputFile(FileName, Kind, Input.isSystem());
}
if (CI.getLangOpts().ModuleName.empty())
CI.getLangOpts().ModuleName = std::string(FileName);
CI.getLangOpts().CurrentModule = CI.getLangOpts().ModuleName;
}
if (!CI.InitializeSourceManager(Input))
return false;
if (CI.getLangOpts().CPlusPlusModules && Input.getKind().isHeaderUnit() &&
Input.getKind().isPreprocessed() && !usesPreprocessorOnly()) {
std::string PresumedInputFile = std::string(getCurrentFileOrBufferName());
ReadOriginalFileName(CI, PresumedInputFile);
if (CI.getLangOpts().ModuleName.empty())
CI.getLangOpts().ModuleName = std::string(PresumedInputFile);
CI.getLangOpts().CurrentModule = CI.getLangOpts().ModuleName;
}
if (Input.getKind().getFormat() == InputKind::ModuleMap) {
CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleMap);
std::string PresumedModuleMapFile;
unsigned OffsetToContents;
if (loadModuleMapForModuleBuild(CI, Input.isSystem(),
Input.isPreprocessed(),
PresumedModuleMapFile, OffsetToContents))
return false;
auto *CurrentModule = prepareToBuildModule(CI, Input.getFile());
if (!CurrentModule)
return false;
CurrentModule->PresumedModuleMapFile = PresumedModuleMapFile;
if (OffsetToContents)
CI.getPreprocessor().setSkipMainFilePreamble(OffsetToContents, true);
else {
auto Buffer = getInputBufferForModule(CI, CurrentModule);
if (!Buffer)
return false;
auto Kind = CurrentModule->IsSystem ? SrcMgr::C_System : SrcMgr::C_User;
auto &SourceMgr = CI.getSourceManager();
auto BufferID = SourceMgr.createFileID(std::move(Buffer), Kind);
assert(BufferID.isValid() && "couldn't create module buffer ID");
SourceMgr.setMainFileID(BufferID);
}
}
if (!BeginSourceFileAction(CI))
return false;
for (const auto &Filename : CI.getFrontendOpts().ModuleMapFiles) {
if (auto File = CI.getFileManager().getFile(Filename))
CI.getPreprocessor().getHeaderSearchInfo().loadModuleMapFile(
*File, false);
else
CI.getDiagnostics().Report(diag::err_module_map_not_found) << Filename;
}
CI.getPreprocessor()
.getHeaderSearchInfo()
.getModuleMap()
.finishModuleDeclarationScope();
if (!usesPreprocessorOnly()) {
if (!isModelParsingAction())
CI.createASTContext();
std::string PresumedInputFile = std::string(getCurrentFileOrBufferName());
if (Input.isPreprocessed())
ReadOriginalFileName(CI, PresumedInputFile);
std::unique_ptr<ASTConsumer> Consumer =
CreateWrappedASTConsumer(CI, PresumedInputFile);
if (!Consumer)
return false;
if (!isModelParsingAction())
CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
IntrusiveRefCntPtr<ExternalSemaSource> source, FinalReader;
source = createChainedIncludesSource(CI, FinalReader);
if (!source)
return false;
CI.setASTReader(static_cast<ASTReader *>(FinalReader.get()));
CI.getASTContext().setExternalSource(source);
} else if (CI.getLangOpts().Modules ||
!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
assert(hasPCHSupport() && "This action does not have PCH support!");
ASTDeserializationListener *DeserialListener =
Consumer->GetASTDeserializationListener();
bool DeleteDeserialListener = false;
if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls) {
DeserialListener = new DeserializedDeclsDumper(DeserialListener,
DeleteDeserialListener);
DeleteDeserialListener = true;
}
if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty()) {
DeserialListener = new DeserializedDeclsChecker(
CI.getASTContext(),
CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
DeserialListener, DeleteDeserialListener);
DeleteDeserialListener = true;
}
if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
CI.createPCHExternalASTSource(
CI.getPreprocessorOpts().ImplicitPCHInclude,
CI.getPreprocessorOpts().DisablePCHOrModuleValidation,
CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
DeserialListener, DeleteDeserialListener);
if (!CI.getASTContext().getExternalSource())
return false;
}
if (CI.getLangOpts().Modules || !CI.hasASTContext() ||
!CI.getASTContext().getExternalSource()) {
CI.createASTReader();
CI.getASTReader()->setDeserializationListener(DeserialListener,
DeleteDeserialListener);
}
}
CI.setASTConsumer(std::move(Consumer));
if (!CI.hasASTConsumer())
return false;
}
if (CI.getLangOpts().Modules || !CI.hasASTContext() ||
!CI.getASTContext().getExternalSource()) {
Preprocessor &PP = CI.getPreprocessor();
PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
PP.getLangOpts());
} else {
assert((!CI.getLangOpts().Modules || CI.getASTReader()) &&
"modules enabled but created an external source that "
"doesn't support modules");
}
for (const auto &ModuleFile : CI.getFrontendOpts().ModuleFiles)
if (!CI.loadModuleFile(ModuleFile))
return false;
if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
IntrusiveRefCntPtr<ExternalASTSource>
Override(new LayoutOverrideSource(
CI.getFrontendOpts().OverrideRecordLayoutsFile));
CI.getASTContext().setExternalSource(Override);
}
if (CI.getLangOpts().HLSL && CI.hasASTContext()) {
IntrusiveRefCntPtr<ExternalASTSource> HLSLSema(
new HLSLExternalSemaSource());
CI.getASTContext().setExternalSource(HLSLSema);
}
FailureCleanup.release();
return true;
}
llvm::Error FrontendAction::Execute() {
CompilerInstance &CI = getCompilerInstance();
if (CI.hasFrontendTimer()) {
llvm::TimeRegion Timer(CI.getFrontendTimer());
ExecuteAction();
}
else ExecuteAction();
if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
CI.hasPreprocessor()) {
StringRef Cache =
CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
if (!Cache.empty()) {
if (llvm::Error Err = GlobalModuleIndex::writeIndex(
CI.getFileManager(), CI.getPCHContainerReader(), Cache)) {
consumeError(std::move(Err));
}
}
}
return llvm::Error::success();
}
void FrontendAction::EndSourceFile() {
CompilerInstance &CI = getCompilerInstance();
CI.getDiagnosticClient().EndSourceFile();
if (CI.hasPreprocessor())
CI.getPreprocessor().EndSourceFile();
EndSourceFileAction();
bool DisableFree = CI.getFrontendOpts().DisableFree;
if (DisableFree) {
CI.resetAndLeakSema();
CI.resetAndLeakASTContext();
llvm::BuryPointer(CI.takeASTConsumer().get());
} else {
CI.setSema(nullptr);
CI.setASTContext(nullptr);
CI.setASTConsumer(nullptr);
}
if (CI.getFrontendOpts().ShowStats) {
llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFileOrBufferName() << "':\n";
CI.getPreprocessor().PrintStats();
CI.getPreprocessor().getIdentifierTable().PrintStats();
CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
CI.getSourceManager().PrintStats();
llvm::errs() << "\n";
}
CI.clearOutputFiles(shouldEraseOutputFiles());
if (isCurrentFileAST()) {
if (DisableFree) {
CI.resetAndLeakPreprocessor();
CI.resetAndLeakSourceManager();
CI.resetAndLeakFileManager();
llvm::BuryPointer(std::move(CurrentASTUnit));
} else {
CI.setPreprocessor(nullptr);
CI.setSourceManager(nullptr);
CI.setFileManager(nullptr);
}
}
setCompilerInstance(nullptr);
setCurrentInput(FrontendInputFile());
CI.getLangOpts().setCompilingModule(LangOptions::CMK_None);
}
bool FrontendAction::shouldEraseOutputFiles() {
return getCompilerInstance().getDiagnostics().hasErrorOccurred();
}
void ASTFrontendAction::ExecuteAction() {
CompilerInstance &CI = getCompilerInstance();
if (!CI.hasPreprocessor())
return;
if (hasCodeCompletionSupport() &&
!CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
CI.createCodeCompletionConsumer();
CodeCompleteConsumer *CompletionConsumer = nullptr;
if (CI.hasCodeCompletionConsumer())
CompletionConsumer = &CI.getCodeCompletionConsumer();
if (!CI.hasSema())
CI.createSema(getTranslationUnitKind(), CompletionConsumer);
ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
CI.getFrontendOpts().SkipFunctionBodies);
}
void PluginASTAction::anchor() { }
std::unique_ptr<ASTConsumer>
PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
StringRef InFile) {
llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
}
bool WrapperFrontendAction::PrepareToExecuteAction(CompilerInstance &CI) {
return WrappedAction->PrepareToExecuteAction(CI);
}
std::unique_ptr<ASTConsumer>
WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
StringRef InFile) {
return WrappedAction->CreateASTConsumer(CI, InFile);
}
bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
return WrappedAction->BeginInvocation(CI);
}
bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI) {
WrappedAction->setCurrentInput(getCurrentInput());
WrappedAction->setCompilerInstance(&CI);
auto Ret = WrappedAction->BeginSourceFileAction(CI);
setCurrentInput(WrappedAction->getCurrentInput());
return Ret;
}
void WrapperFrontendAction::ExecuteAction() {
WrappedAction->ExecuteAction();
}
void WrapperFrontendAction::EndSourceFile() { WrappedAction->EndSourceFile(); }
void WrapperFrontendAction::EndSourceFileAction() {
WrappedAction->EndSourceFileAction();
}
bool WrapperFrontendAction::shouldEraseOutputFiles() {
return WrappedAction->shouldEraseOutputFiles();
}
bool WrapperFrontendAction::usesPreprocessorOnly() const {
return WrappedAction->usesPreprocessorOnly();
}
TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
return WrappedAction->getTranslationUnitKind();
}
bool WrapperFrontendAction::hasPCHSupport() const {
return WrappedAction->hasPCHSupport();
}
bool WrapperFrontendAction::hasASTFileSupport() const {
return WrappedAction->hasASTFileSupport();
}
bool WrapperFrontendAction::hasIRSupport() const {
return WrappedAction->hasIRSupport();
}
bool WrapperFrontendAction::hasCodeCompletionSupport() const {
return WrappedAction->hasCodeCompletionSupport();
}
WrapperFrontendAction::WrapperFrontendAction(
std::unique_ptr<FrontendAction> WrappedAction)
: WrappedAction(std::move(WrappedAction)) {}