Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/DiagnosticFrontendKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def note_fe_backend_resource_limit: Note<"%0 (%1) exceeds limit (%2) in '%3'">,
def remark_fe_backend_plugin: Remark<"%0">, BackendInfo, InGroup<RemarkBackendPlugin>;
def note_fe_backend_plugin: Note<"%0">, BackendInfo;

def err_spirv_requires_vulkan : Error<
"SPIR-V target requires a Vulkan environment (e.g. '-target spirv64-unknown-vulkan1.3')">;

def warn_fe_override_module : Warning<
"overriding the module target triple with %0">,
InGroup<DiagGroup<"override-module">>;
Expand Down
105 changes: 54 additions & 51 deletions clang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ void CompilerInstance::setVerboseOutputStream(raw_ostream &Value) {
VerboseOutputStream = &Value;
}

void CompilerInstance::setVerboseOutputStream(std::unique_ptr<raw_ostream> Value) {
void CompilerInstance::setVerboseOutputStream(
std::unique_ptr<raw_ostream> Value) {
OwnedVerboseOutputStream.swap(Value);
VerboseOutputStream = OwnedVerboseOutputStream.get();
}
Expand All @@ -112,6 +113,17 @@ void CompilerInstance::setTarget(TargetInfo *Value) { Target = Value; }
void CompilerInstance::setAuxTarget(TargetInfo *Value) { AuxTarget = Value; }

bool CompilerInstance::createTarget() {

// Validate Vulkan environment for SPIRV.
llvm::Triple Triple(getInvocation().getTargetOpts().Triple);
if (Triple.getArch() == llvm::Triple::spirv) {
if (Triple.getOS() != llvm::Triple::Vulkan ||
Triple.getVulkanVersion() == llvm::VersionTuple(0)) {
getDiagnostics().Report(diag::err_spirv_requires_vulkan) << Triple.str();
return false;
}
}

// Create the target instance.
setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(),
getInvocation().getTargetOpts()));
Expand Down Expand Up @@ -186,9 +198,7 @@ void CompilerInstance::setASTContext(
getASTConsumer().Initialize(getASTContext());
}

void CompilerInstance::setSema(Sema *S) {
TheSema.reset(S);
}
void CompilerInstance::setSema(Sema *S) { TheSema.reset(S); }

void CompilerInstance::setASTConsumer(std::unique_ptr<ASTConsumer> Value) {
Consumer = std::move(Value);
Expand Down Expand Up @@ -316,7 +326,7 @@ static void SetUpDiagnosticLog(DiagnosticOptions &DiagOpts,

// Chain in the diagnostic client which will log the diagnostics.
auto Logger = std::make_unique<LogDiagnosticPrinter>(*OS, DiagOpts,
std::move(StreamOwner));
std::move(StreamOwner));
if (CodeGenOpts)
Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags);
if (Diags.ownsClient()) {
Expand Down Expand Up @@ -597,7 +607,8 @@ struct ReadModuleNames : ASTReaderListener {
Stack.push_back(M);
while (!Stack.empty()) {
Module *Current = Stack.pop_back_val();
if (Current->IsUnimportable) continue;
if (Current->IsUnimportable)
continue;
Current->IsAvailable = true;
auto SubmodulesRange = Current->submodules();
llvm::append_range(Stack, SubmodulesRange);
Expand Down Expand Up @@ -691,16 +702,14 @@ IntrusiveRefCntPtr<ASTReader> CompilerInstance::createPCHExternalASTSource(

// Code Completion

static bool EnableCodeCompletion(Preprocessor &PP,
StringRef Filename,
unsigned Line,
unsigned Column) {
static bool EnableCodeCompletion(Preprocessor &PP, StringRef Filename,
unsigned Line, unsigned Column) {
// Tell the source manager to chop off the given file at a specific
// line and column.
auto Entry = PP.getFileManager().getOptionalFileRef(Filename);
if (!Entry) {
PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
<< Filename;
<< Filename;
return true;
}

Expand All @@ -716,8 +725,8 @@ void CompilerInstance::createCodeCompletionConsumer() {
getPreprocessor(), Loc.FileName, Loc.Line, Loc.Column,
getFrontendOpts().CodeCompleteOpts, llvm::outs()));
return;
} else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName,
Loc.Line, Loc.Column)) {
} else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName, Loc.Line,
Loc.Column)) {
setCodeCompletionConsumer(nullptr);
return;
}
Expand All @@ -728,13 +737,9 @@ void CompilerInstance::createFrontendTimer() {
FrontendTimer.reset(new llvm::Timer("frontend", "Front end", *timerGroup));
}

CodeCompleteConsumer *
CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
StringRef Filename,
unsigned Line,
unsigned Column,
const CodeCompleteOptions &Opts,
raw_ostream &OS) {
CodeCompleteConsumer *CompilerInstance::createCodeCompletionConsumer(
Preprocessor &PP, StringRef Filename, unsigned Line, unsigned Column,
const CodeCompleteOptions &Opts, raw_ostream &OS) {
if (EnableCodeCompletion(PP, Filename, Line, Column))
return nullptr;

Expand Down Expand Up @@ -896,7 +901,7 @@ CompilerInstance::createOutputFileImpl(StringRef OutputPath, bool Binary,

// Initialization Utilities

bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input){
bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input) {
return InitializeSourceManager(Input, getDiagnostics(), getFileManager(),
getSourceManager());
}
Expand All @@ -910,7 +915,8 @@ bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input,
Input.getKind().getFormat() == InputKind::ModuleMap
? Input.isSystem() ? SrcMgr::C_System_ModuleMap
: SrcMgr::C_User_ModuleMap
: Input.isSystem() ? SrcMgr::C_System : SrcMgr::C_User;
: Input.isSystem() ? SrcMgr::C_System
: SrcMgr::C_User;

if (Input.isBuffer()) {
SourceMgr.setMainFileID(SourceMgr.createFileID(Input.getBuffer(), Kind));
Expand Down Expand Up @@ -1441,7 +1447,7 @@ static bool readASTAfterCompileModule(CompilerInstance &ImportingInstance,
// The ASTReader didn't diagnose the error, so conservatively report it.
if (ReadResult == ASTReader::Missing || !Diags.hasErrorOccurred())
Diags.Report(ModuleNameLoc, diag::err_module_not_built)
<< Module->Name << SourceRange(ImportLoc, ModuleNameLoc);
<< Module->Name << SourceRange(ImportLoc, ModuleNameLoc);

return false;
}
Expand Down Expand Up @@ -1660,29 +1666,29 @@ static void checkConfigMacro(Preprocessor &PP, StringRef ConfigMacro,
// This macro was defined on the command line, then #undef'd later.
// Complain.
PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
<< true << ConfigMacro << Mod->getFullModuleName();
<< true << ConfigMacro << Mod->getFullModuleName();
auto LatestDef = LatestLocalMD->getDefinition();
assert(LatestDef.isUndefined() &&
"predefined macro went away with no #undef?");
PP.Diag(LatestDef.getUndefLocation(), diag::note_module_def_undef_here)
<< true;
<< true;
return;
} else if (!CmdLineDefinition) {
// There was no definition for this macro in the command line,
// but there was a local definition. Complain.
PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
<< false << ConfigMacro << Mod->getFullModuleName();
<< false << ConfigMacro << Mod->getFullModuleName();
PP.Diag(CurrentDefinition->getDefinitionLoc(),
diag::note_module_def_undef_here)
<< false;
<< false;
} else if (!CurrentDefinition->isIdenticalTo(*CmdLineDefinition, PP,
/*Syntactically=*/true)) {
// The macro definitions differ.
PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
<< false << ConfigMacro << Mod->getFullModuleName();
<< false << ConfigMacro << Mod->getFullModuleName();
PP.Diag(CurrentDefinition->getDefinitionLoc(),
diag::note_module_def_undef_here)
<< false;
<< false;
}
}

Expand Down Expand Up @@ -1737,7 +1743,7 @@ void CompilerInstance::createASTReader() {
TheASTReader->setDeserializationListener(
getASTConsumer().GetASTDeserializationListener());
getASTContext().setASTMutationListener(
getASTConsumer().GetASTMutationListener());
getASTConsumer().GetASTMutationListener());
}
getASTContext().setExternalSource(TheASTReader);
if (hasSema())
Expand Down Expand Up @@ -1899,16 +1905,15 @@ ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST(
unsigned ARRFlags = Source == MS_ModuleCache
? ASTReader::ARR_OutOfDate | ASTReader::ARR_Missing |
ASTReader::ARR_TreatModuleWithErrorsAsOutOfDate
: Source == MS_PrebuiltModulePath
? 0
: ASTReader::ARR_ConfigurationMismatch;
switch (getASTReader()->ReadAST(ModuleFilename,
Source == MS_PrebuiltModulePath
? serialization::MK_PrebuiltModule
: Source == MS_ModuleBuildPragma
? serialization::MK_ExplicitModule
: serialization::MK_ImplicitModule,
ImportLoc, ARRFlags)) {
: Source == MS_PrebuiltModulePath
? 0
: ASTReader::ARR_ConfigurationMismatch;
switch (getASTReader()->ReadAST(
ModuleFilename,
Source == MS_PrebuiltModulePath ? serialization::MK_PrebuiltModule
: Source == MS_ModuleBuildPragma ? serialization::MK_ExplicitModule
: serialization::MK_ImplicitModule,
ImportLoc, ARRFlags)) {
case ASTReader::Success: {
if (M)
return M;
Expand Down Expand Up @@ -2008,8 +2013,7 @@ ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST(
}

ModuleLoadResult
CompilerInstance::loadModule(SourceLocation ImportLoc,
ModuleIdPath Path,
CompilerInstance::loadModule(SourceLocation ImportLoc, ModuleIdPath Path,
Module::NameVisibilityKind Visibility,
bool IsInclusionDirective) {
// Determine what file we're searching from.
Expand Down Expand Up @@ -2244,7 +2248,7 @@ void CompilerInstance::createModuleFromSource(SourceLocation ImportLoc,
FrontendInputFile Input(
ModuleMapFileName,
InputKind(getLanguageFromOptions(Invocation->getLangOpts()),
InputKind::ModuleMap, /*Preprocessed*/true));
InputKind::ModuleMap, /*Preprocessed*/ true));

std::string NullTerminatedSource(Source.str());

Expand Down Expand Up @@ -2285,8 +2289,8 @@ void CompilerInstance::makeModuleVisible(Module *Mod,
TheASTReader->makeModuleVisible(Mod, Visibility, ImportLoc);
}

GlobalModuleIndex *CompilerInstance::loadGlobalModuleIndex(
SourceLocation TriggerLoc) {
GlobalModuleIndex *
CompilerInstance::loadGlobalModuleIndex(SourceLocation TriggerLoc) {
if (getPreprocessor()
.getHeaderSearchInfo()
.getSpecificModuleCachePath()
Expand Down Expand Up @@ -2332,7 +2336,8 @@ GlobalModuleIndex *CompilerInstance::loadGlobalModuleIndex(

bool RecreateIndex = false;
for (ModuleMap::module_iterator I = MMap.module_begin(),
E = MMap.module_end(); I != E; ++I) {
E = MMap.module_end();
I != E; ++I) {
Module *TheModule = I->second;
if (!TheModule->getASTFileKey()) {
SmallVector<IdentifierLoc, 2> Path;
Expand Down Expand Up @@ -2364,15 +2369,13 @@ GlobalModuleIndex *CompilerInstance::loadGlobalModuleIndex(
}

// Check global module index for missing imports.
bool
CompilerInstance::lookupMissingImports(StringRef Name,
SourceLocation TriggerLoc) {
bool CompilerInstance::lookupMissingImports(StringRef Name,
SourceLocation TriggerLoc) {
// Look for the symbol in non-imported modules, but only if an error
// actually occurred.
if (!buildingModule()) {
// Load global module index, or retrieve a previously loaded one.
GlobalModuleIndex *GlobalIndex = loadGlobalModuleIndex(
TriggerLoc);
GlobalModuleIndex *GlobalIndex = loadGlobalModuleIndex(TriggerLoc);

// Only if we have a global index.
if (GlobalIndex) {
Expand Down
4 changes: 4 additions & 0 deletions clang/test/Driver/spirv-target-validation.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// RUN: not %clang -target spirv %s 2>&1 | FileCheck %s
// CHECK: error: SPIR-V target requires a Vulkan environment

int main() { return 0; }