Skip to content

Commit 5834996

Browse files
bnbarhamakyrtzi
authored andcommitted
[Frontend] Add flag to allow PCM generation despite compiler errors
As with precompiled headers, it's useful for indexers to be able to continue through compiler errors in dependent modules. Resolves rdar://69816264 Reviewed By: akyrtzi Differential Revision: https://reviews.llvm.org/D91580
1 parent f4c6080 commit 5834996

File tree

13 files changed

+62
-10
lines changed

13 files changed

+62
-10
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4356,6 +4356,8 @@ def fno_validate_pch : Flag<["-"], "fno-validate-pch">,
43564356
HelpText<"Disable validation of precompiled headers">;
43574357
def fallow_pch_with_errors : Flag<["-"], "fallow-pch-with-compiler-errors">,
43584358
HelpText<"Accept a PCH file that was created with compiler errors">;
4359+
def fallow_pcm_with_errors : Flag<["-"], "fallow-pcm-with-compiler-errors">,
4360+
HelpText<"Accept a PCM file that was created with compiler errors">;
43594361
def dump_deserialized_pch_decls : Flag<["-"], "dump-deserialized-decls">,
43604362
HelpText<"Dump declarations that are deserialized from PCH, for testing">;
43614363
def error_on_deserialized_pch_decl : Separate<["-"], "error-on-deserialized-decl">,

clang/include/clang/Frontend/ASTUnit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ class ASTUnit {
694694
const FileSystemOptions &FileSystemOpts, bool UseDebugInfo = false,
695695
bool OnlyLocalDecls = false, ArrayRef<RemappedFile> RemappedFiles = None,
696696
CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
697-
bool AllowPCHWithCompilerErrors = false,
697+
bool AllowASTWithCompilerErrors = false,
698698
bool UserFilesAreVolatile = false);
699699

700700
private:

clang/include/clang/Frontend/FrontendActions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ class GenerateModuleAction : public ASTFrontendAction {
117117
}
118118

119119
bool hasASTFileSupport() const override { return false; }
120+
121+
bool shouldEraseOutputFiles() override;
120122
};
121123

122124
class GenerateInterfaceStubsAction : public ASTFrontendAction {

clang/include/clang/Frontend/FrontendOptions.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ class FrontendOptions {
303303
/// When using -emit-module, treat the modulemap as a system module.
304304
unsigned IsSystemModule : 1;
305305

306+
/// Output (and read) PCM files regardless of compiler errors.
307+
unsigned AllowPCMWithCompilerErrors : 1;
308+
306309
CodeCompleteOptions CodeCompleteOpts;
307310

308311
/// Specifies the output format of the AST.
@@ -457,7 +460,8 @@ class FrontendOptions {
457460
UseGlobalModuleIndex(true), GenerateGlobalModuleIndex(true),
458461
ASTDumpDecls(false), ASTDumpLookups(false),
459462
BuildingImplicitModule(false), ModulesEmbedAllFiles(false),
460-
IncludeTimestamps(true), UseTemporary(true), TimeTraceGranularity(500) {}
463+
IncludeTimestamps(true), UseTemporary(true),
464+
AllowPCMWithCompilerErrors(false), TimeTraceGranularity(500) {}
461465

462466
/// getInputKindForExtension - Return the appropriate input kind for a file
463467
/// extension. For example, "c" would return Language::C.

clang/lib/Frontend/ASTUnit.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
759759
WhatToLoad ToLoad, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
760760
const FileSystemOptions &FileSystemOpts, bool UseDebugInfo,
761761
bool OnlyLocalDecls, ArrayRef<RemappedFile> RemappedFiles,
762-
CaptureDiagsKind CaptureDiagnostics, bool AllowPCHWithCompilerErrors,
762+
CaptureDiagsKind CaptureDiagnostics, bool AllowASTWithCompilerErrors,
763763
bool UserFilesAreVolatile) {
764764
std::unique_ptr<ASTUnit> AST(new ASTUnit(true));
765765

@@ -819,7 +819,7 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
819819
AST->Reader = new ASTReader(
820820
PP, *AST->ModuleCache, AST->Ctx.get(), PCHContainerRdr, {},
821821
/*isysroot=*/"",
822-
/*DisableValidation=*/disableValid, AllowPCHWithCompilerErrors);
822+
/*DisableValidation=*/disableValid, AllowASTWithCompilerErrors);
823823

824824
AST->Reader->setListener(std::make_unique<ASTInfoCollector>(
825825
*AST->PP, AST->Ctx.get(), *AST->HSOpts, *AST->PPOpts, *AST->LangOpts,

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,9 @@ void CompilerInstance::createASTReader() {
15231523
HeaderSearchOptions &HSOpts = getHeaderSearchOpts();
15241524
std::string Sysroot = HSOpts.Sysroot;
15251525
const PreprocessorOptions &PPOpts = getPreprocessorOpts();
1526+
const FrontendOptions &FEOpts = getFrontendOpts();
15261527
std::unique_ptr<llvm::Timer> ReadTimer;
1528+
15271529
if (FrontendTimerGroup)
15281530
ReadTimer = std::make_unique<llvm::Timer>("reading_modules",
15291531
"Reading modules",
@@ -1532,7 +1534,7 @@ void CompilerInstance::createASTReader() {
15321534
getPreprocessor(), getModuleCache(), &getASTContext(),
15331535
getPCHContainerReader(), getFrontendOpts().ModuleFileExtensions,
15341536
Sysroot.empty() ? "" : Sysroot.c_str(), PPOpts.DisablePCHValidation,
1535-
/*AllowASTWithCompilerErrors=*/false,
1537+
/*AllowASTWithCompilerErrors=*/FEOpts.AllowPCMWithCompilerErrors,
15361538
/*AllowConfigurationMismatch=*/false, HSOpts.ModulesValidateSystemHeaders,
15371539
HSOpts.ValidateASTInputFilesContent,
15381540
getFrontendOpts().UseGlobalModuleIndex, std::move(ReadTimer));

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2027,6 +2027,7 @@ static InputKind ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
20272027
Opts.IncludeTimestamps = !Args.hasArg(OPT_fno_pch_timestamp);
20282028
Opts.UseTemporary = !Args.hasArg(OPT_fno_temp_file);
20292029
Opts.IsSystemModule = Args.hasArg(OPT_fsystem_module);
2030+
Opts.AllowPCMWithCompilerErrors = Args.hasArg(OPT_fallow_pcm_with_errors);
20302031

20312032
if (Opts.ProgramAction != frontend::GenerateModule && Opts.IsSystemModule)
20322033
Diags.Report(diag::err_drv_argument_only_allowed_with) << "-fsystem-module"
@@ -3611,7 +3612,8 @@ static void ParsePreprocessorArgs(PreprocessorOptions &Opts, ArgList &Args,
36113612
Opts.UsePredefines = !Args.hasArg(OPT_undef);
36123613
Opts.DetailedRecord = Args.hasArg(OPT_detailed_preprocessing_record);
36133614
Opts.DisablePCHValidation = Args.hasArg(OPT_fno_validate_pch);
3614-
Opts.AllowPCHWithCompilerErrors = Args.hasArg(OPT_fallow_pch_with_errors);
3615+
Opts.AllowPCHWithCompilerErrors =
3616+
Args.hasArg(OPT_fallow_pch_with_errors, OPT_fallow_pcm_with_errors);
36153617

36163618
Opts.DumpDeserializedPCHDecls = Args.hasArg(OPT_dump_deserialized_pch_decls);
36173619
for (const auto *A : Args.filtered(OPT_error_on_deserialized_pch_decl))

clang/lib/Frontend/FrontendActions.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
177177
Consumers.push_back(std::make_unique<PCHGenerator>(
178178
CI.getPreprocessor(), CI.getModuleCache(), OutputFile, Sysroot, Buffer,
179179
CI.getFrontendOpts().ModuleFileExtensions,
180-
/*AllowASTWithErrors=*/false,
180+
/*AllowASTWithErrors=*/
181+
+CI.getFrontendOpts().AllowPCMWithCompilerErrors,
181182
/*IncludeTimestamps=*/
182183
+CI.getFrontendOpts().BuildingImplicitModule,
183184
/*ShouldCacheASTInMemory=*/
@@ -187,6 +188,11 @@ GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
187188
return std::make_unique<MultiplexConsumer>(std::move(Consumers));
188189
}
189190

191+
bool GenerateModuleAction::shouldEraseOutputFiles() {
192+
return !getCompilerInstance().getFrontendOpts().AllowPCMWithCompilerErrors &&
193+
ASTFrontendAction::shouldEraseOutputFiles();
194+
}
195+
190196
bool GenerateModuleFromModuleMapAction::BeginSourceFileAction(
191197
CompilerInstance &CI) {
192198
if (!CI.getLangOpts().Modules) {
@@ -339,7 +345,7 @@ void VerifyPCHAction::ExecuteAction() {
339345
CI.getPCHContainerReader(), CI.getFrontendOpts().ModuleFileExtensions,
340346
Sysroot.empty() ? "" : Sysroot.c_str(),
341347
/*DisableValidation*/ false,
342-
/*AllowPCHWithCompilerErrors*/ false,
348+
/*AllowASTWithCompilerErrors*/ false,
343349
/*AllowConfigurationMismatch*/ true,
344350
/*ValidateSystemInputs*/ true));
345351

clang/test/Modules/Inputs/error.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@import undefined
2+
3+
@interface Error
4+
- (int)method;
5+
undefined
6+
@end
7+
8+
undefined

clang/test/Modules/Inputs/module.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,3 +483,4 @@ module template_nontrivial1 {
483483
header "template-nontrivial1.h"
484484
export *
485485
}
486+
module error { header "error.h" }
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir %t
3+
4+
// Write out a module with errors make sure it can be read
5+
// RUN: %clang_cc1 -fmodules -fallow-pcm-with-compiler-errors \
6+
// RUN: -fmodules-cache-path=%t -x objective-c -emit-module \
7+
// RUN: -fmodule-name=error %S/Inputs/module.map
8+
// RUN: %clang_cc1 -fmodules -fallow-pcm-with-compiler-errors \
9+
// RUN: -fmodules-cache-path=%t -x objective-c -I %S/Inputs \
10+
// RUN: -fimplicit-module-maps -ast-print %s | FileCheck %s
11+
12+
// allow-pcm-with-compiler-errors should also allow errors in PCH
13+
// RUN: %clang_cc1 -fallow-pcm-with-compiler-errors -x c++ -emit-pch \
14+
// RUN: -o %t/check.pch %S/Inputs/error.h
15+
16+
@import error;
17+
18+
void test(id x) {
19+
[x method];
20+
}
21+
22+
// CHECK: @interface Error
23+
// CHECK-NEXT: - (int)method;
24+
// CHECK-NEXT: @end
25+
// CHECK: void test(id x)

clang/tools/c-index-test/core_main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ static bool printSourceSymbolsFromModule(StringRef modulePath,
263263
std::string(modulePath), *pchRdr, ASTUnit::LoadASTOnly, Diags,
264264
FileSystemOpts, /*UseDebugInfo=*/false,
265265
/*OnlyLocalDecls=*/true, None, CaptureDiagsKind::None,
266-
/*AllowPCHWithCompilerErrors=*/true,
266+
/*AllowASTWithCompilerErrors=*/true,
267267
/*UserFilesAreVolatile=*/false);
268268
if (!AU) {
269269
errs() << "failed to create TU for: " << modulePath << '\n';

clang/tools/libclang/CIndex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3476,7 +3476,7 @@ enum CXErrorCode clang_createTranslationUnit2(CXIndex CIdx,
34763476
ast_filename, CXXIdx->getPCHContainerOperations()->getRawReader(),
34773477
ASTUnit::LoadEverything, Diags, FileSystemOpts, /*UseDebugInfo=*/false,
34783478
CXXIdx->getOnlyLocalDecls(), None, CaptureDiagsKind::All,
3479-
/*AllowPCHWithCompilerErrors=*/true,
3479+
/*AllowASTWithCompilerErrors=*/true,
34803480
/*UserFilesAreVolatile=*/true);
34813481
*out_TU = MakeCXTranslationUnit(CXXIdx, std::move(AU));
34823482
return *out_TU ? CXError_Success : CXError_Failure;

0 commit comments

Comments
 (0)