Skip to content

Commit 247b59a

Browse files
authored
Merge pull request #8655 from bnbarham/missing-cherry-pick
[rebranch][cas] Add cc1 option -finclude-tree-preserve-pch-path
2 parents c9d9c31 + 56406bd commit 247b59a

File tree

10 files changed

+113
-24
lines changed

10 files changed

+113
-24
lines changed

clang/include/clang/CAS/IncludeTree.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -794,12 +794,12 @@ class IncludeTreeRoot : public IncludeTreeBase<IncludeTreeRoot> {
794794
return IncludeTree::FileList(std::move(*Node));
795795
}
796796

797-
Expected<std::optional<StringRef>> getPCHBuffer() {
797+
Expected<std::optional<IncludeTree::File>> getPCH() {
798798
if (std::optional<ObjectRef> Ref = getPCHRef()) {
799799
auto Node = getCAS().getProxy(*Ref);
800800
if (!Node)
801801
return Node.takeError();
802-
return Node->getData();
802+
return IncludeTree::File(std::move(*Node));
803803
}
804804
return std::nullopt;
805805
}

clang/include/clang/Driver/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8212,6 +8212,11 @@ defm casid_output : BoolFOption<"casid-output",
82128212
" write a CASID for the output file.">,
82138213
NegFlag<SetFalse>>;
82148214

8215+
defm include_tree_preserve_pch_path : BoolFOption<"include-tree-preserve-pch-path",
8216+
FrontendOpts<"IncludeTreePreservePCHPath">, DefaultFalse,
8217+
PosFlag<SetTrue, [], [], "Keep the original PCH path in include-tree rather than canonicalizing">,
8218+
NegFlag<SetFalse>>;
8219+
82158220
/// BEGIN MCCAS
82168221
defm cas_backend : BoolFOption<"cas-backend",
82178222
CodeGenOpts<"UseCASBackend">, DefaultFalse,

clang/include/clang/Frontend/FrontendOptions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,11 @@ class FrontendOptions {
415415
/// caching of compilation outputs. This is used for testing purposes.
416416
unsigned DisableCachedCompileJobReplay : 1;
417417

418+
/// Whether to preserve the original PCH path in the include-tree, or to
419+
/// canonicalize it to a fixed value. Setting this to \c true allows the use
420+
/// of gmodules with PCH and include tree.
421+
unsigned IncludeTreePreservePCHPath : 1;
422+
418423
/// Keep the diagnostic client open for receiving diagnostics after the source
419424
/// files have been processed.
420425
unsigned MayEmitDiagnosticsAfterProcessingSourceFiles : 1;
@@ -642,6 +647,7 @@ class FrontendOptions {
642647
BuildingImplicitModuleUsesLock(true), ModulesEmbedAllFiles(false),
643648
IncludeTimestamps(true), UseTemporary(true), CacheCompileJob(false),
644649
ForIncludeTreeScan(false), DisableCachedCompileJobReplay(false),
650+
IncludeTreePreservePCHPath(false),
645651
MayEmitDiagnosticsAfterProcessingSourceFiles(false), WriteOutputAsCASID(false),
646652
AllowPCMWithCompilerErrors(false), ModulesShareFileManager(true),
647653
EmitSymbolGraph(false), EmitExtensionSymbolGraphs(false),

clang/lib/CAS/IncludeTree.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -832,10 +832,13 @@ llvm::Error IncludeTree::APINotes::forEachAPINotes(
832832
}
833833

834834
llvm::Error IncludeTreeRoot::print(llvm::raw_ostream &OS, unsigned Indent) {
835-
if (std::optional<ObjectRef> PCHRef = getPCHRef()) {
835+
std::optional<IncludeTree::File> PCH;
836+
if (llvm::Error E = getPCH().moveInto(PCH))
837+
return E;
838+
if (PCH) {
836839
OS.indent(Indent) << "(PCH) ";
837-
getCAS().getID(*PCHRef).print(OS);
838-
OS << '\n';
840+
if (llvm::Error E = PCH->print(OS))
841+
return E;
839842
}
840843
std::optional<cas::IncludeTree> MainTree;
841844
if (llvm::Error E = getMainFileTree().moveInto(MainTree))

clang/lib/Frontend/FrontendAction.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
10031003
};
10041004

10051005
std::optional<cas::IncludeTreeRoot> IncludeTreeRoot;
1006-
std::optional<StringRef> IncludeTreePCHBuffer;
1006+
std::optional<cas::IncludeTree::File> IncludeTreePCH;
10071007
if (Input.isIncludeTree()) {
10081008
if (llvm::Error E = cas::IncludeTreeRoot::get(CI.getOrCreateObjectStore(),
10091009
Input.getIncludeTree())
@@ -1017,8 +1017,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
10171017
CI.getPreprocessor().setPPCachedActions(std::move(*PPCachedAct));
10181018
CI.getFrontendOpts().IncludeTimestamps = false;
10191019

1020-
if (llvm::Error E =
1021-
IncludeTreeRoot->getPCHBuffer().moveInto(IncludeTreePCHBuffer))
1020+
if (llvm::Error E = IncludeTreeRoot->getPCH().moveInto(IncludeTreePCH))
10221021
return reportError(std::move(E));
10231022

10241023
auto ModMap = IncludeTreeRoot->getModuleMap();
@@ -1153,7 +1152,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
11531152
CI.getASTContext().setExternalSource(source);
11541153
} else if (CI.getLangOpts().Modules ||
11551154
!CI.getPreprocessorOpts().ImplicitPCHInclude.empty() ||
1156-
IncludeTreePCHBuffer) {
1155+
IncludeTreePCH) {
11571156
// Use PCM or PCH.
11581157
assert(hasPCHSupport() && "This action does not have PCH support!");
11591158
ASTDeserializationListener *DeserialListener =
@@ -1172,16 +1171,17 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
11721171
DeleteDeserialListener = true;
11731172
}
11741173
if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty() ||
1175-
IncludeTreePCHBuffer) {
1174+
IncludeTreePCH) {
11761175
StringRef PCHPath;
11771176
DisableValidationForModuleKind DisableValidation;
11781177
std::unique_ptr<llvm::MemoryBuffer> PCHBuffer;
1179-
if (IncludeTreePCHBuffer) {
1180-
PCHPath = "<PCH>";
1178+
if (IncludeTreePCH) {
11811179
// No need to do any validation.
11821180
DisableValidation = DisableValidationForModuleKind::All;
1183-
PCHBuffer =
1184-
llvm::MemoryBuffer::getMemBuffer(*IncludeTreePCHBuffer, PCHPath);
1181+
if (llvm::Error E =
1182+
IncludeTreePCH->getMemoryBuffer().moveInto(PCHBuffer))
1183+
return reportError(std::move(E));
1184+
PCHPath = PCHBuffer->getBufferIdentifier();
11851185
} else {
11861186
PCHPath = CI.getPreprocessorOpts().ImplicitPCHInclude;
11871187
DisableValidation =

clang/lib/Tooling/DependencyScanning/IncludeTreeActionController.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,9 +652,17 @@ IncludeTreeBuilder::finishIncludeTree(CompilerInstance &ScanInstance,
652652
FM.getObjectRefForFileContent(PPOpts.ImplicitPCHInclude);
653653
if (!CASContents)
654654
return llvm::errorCodeToError(CASContents.getError());
655-
PCHRef = **CASContents;
656655

657-
return Error::success();
656+
StringRef PCHFilename = "<PCH>";
657+
if (NewInvocation.getFrontendOpts().IncludeTreePreservePCHPath)
658+
PCHFilename = PPOpts.ImplicitPCHInclude;
659+
660+
auto PCHFile =
661+
cas::IncludeTree::File::create(DB, PCHFilename, **CASContents);
662+
if (!PCHFile)
663+
return PCHFile.takeError();
664+
PCHRef = PCHFile->getRef();
665+
return llvm::Error::success();
658666
};
659667

660668
if (Error E = FinishIncludeTree())

clang/lib/Tooling/DependencyScanning/ScanAndUpdateArgs.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,12 @@ void tooling::dependencies::configureInvocationForCaching(
5858
HSOpts.ResourceDir = std::move(OriginalHSOpts.ResourceDir);
5959
// Preserve fmodule-file options.
6060
HSOpts.PrebuiltModuleFiles = std::move(OriginalHSOpts.PrebuiltModuleFiles);
61+
// Preserve -gmodules (see below for caveats).
62+
HSOpts.ModuleFormat = OriginalHSOpts.ModuleFormat;
6163
HSOpts.UseBuiltinIncludes = false;
6264
HSOpts.UseStandardSystemIncludes = false;
6365
HSOpts.UseStandardCXXIncludes = false;
66+
6467
auto &PPOpts = CI.getPreprocessorOpts();
6568
// We don't need this because we save the contents of the PCH file in the
6669
// include tree root.
@@ -75,12 +78,13 @@ void tooling::dependencies::configureInvocationForCaching(
7578
PPOpts.MacroIncludes.clear();
7679
PPOpts.Includes.clear();
7780
}
78-
// Disable `-gmodules` to avoid debug info referencing a non-existent PCH
79-
// filename.
80-
// NOTE: We'd have to preserve \p HeaderSearchOptions::ModuleFormat (code
81-
// above resets \p HeaderSearchOptions) when properly supporting
82-
// `-gmodules`.
83-
CI.getCodeGenOpts().DebugTypeExtRefs = false;
81+
if (!FrontendOpts.IncludeTreePreservePCHPath) {
82+
// Disable `-gmodules` to avoid debug info referencing a non-existent PCH
83+
// filename.
84+
// FIXME: we should also allow -gmodules if there is no PCH involved.
85+
CI.getCodeGenOpts().DebugTypeExtRefs = false;
86+
HSOpts.ModuleFormat = "raw";
87+
}
8488
// Clear APINotes options.
8589
CI.getAPINotesOpts().ModuleSearchPaths = {};
8690
} else {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// RUN: split-file %s %t
2+
// RUN: sed -e "s|DIR|%/t|g" %t/cdb_pch.json.template > %t/cdb_pch.json
3+
// RUN: sed -e "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json
4+
// RUN: sed -e "s|DIR|%/t|g" %t/cdb_no_preserve.json.template > %t/cdb_no_preserve.json
5+
6+
// RUN: clang-scan-deps -compilation-database %t/cdb_pch.json -format experimental-include-tree-full -cas-path %t/cas > %t/deps_pch.json
7+
// RUN: FileCheck %s -input-file %t/deps_pch.json -DPREFIX=%/t
8+
9+
// CHECK: "-fmodule-format=obj"
10+
// CHECK: "-dwarf-ext-refs"
11+
12+
// RUN: %deps-to-rsp %t/deps_pch.json --tu-index 0 > %t/pch.rsp
13+
// RUN: %clang @%t/pch.rsp
14+
15+
// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-include-tree-full -cas-path %t/cas > %t/deps_tu.json
16+
// RUN: FileCheck %s -input-file %t/deps_tu.json -DPREFIX=%/t
17+
18+
// RUN: %deps-to-rsp %t/deps_tu.json --tu-index 0 > %t/tu.rsp
19+
// RUN: %clang @%t/tu.rsp
20+
21+
// RUN: cat %t/tu.ll | FileCheck %s -check-prefix=LLVMIR -DPREFIX=%/t
22+
// LLVMIR: !DICompileUnit({{.*}}, splitDebugFilename: "prefix.pch"
23+
24+
// Extract include-tree casid
25+
// RUN: cat %t/tu.rsp | sed -E 's|.*"-fcas-include-tree" "(llvmcas://[[:xdigit:]]+)".*|\1|' > %t/tu.casid
26+
27+
// RUN: clang-cas-test -cas %t/cas -print-include-tree @%t/tu.casid | FileCheck %s -check-prefix=INCLUDE_TREE -DPREFIX=%/t
28+
// INCLUDE_TREE: (PCH) [[PREFIX]]/prefix.pch llvmcas://
29+
30+
// RUN: clang-scan-deps -compilation-database %t/cdb_no_preserve.json -format experimental-include-tree-full -cas-path %t/cas > %t/deps_no_preserve.json
31+
// RUN: FileCheck %s -input-file %t/deps_no_preserve.json -DPREFIX=%/t -check-prefix=NO_PRESERVE
32+
33+
// Note: "raw" is the default format, so it will not show up in the arguments.
34+
// NO_PRESERVE-NOT: "-fmodule-format=
35+
// NO_PRESERVE-NOT: "-dwarf-ext-refs"
36+
37+
38+
//--- cdb_pch.json.template
39+
[{
40+
"directory": "DIR",
41+
"command": "clang -x c-header DIR/prefix.h -target x86_64-apple-macos12 -o DIR/prefix.pch -gmodules -g -Xclang -finclude-tree-preserve-pch-path",
42+
"file": "DIR/prefix.h"
43+
}]
44+
45+
//--- cdb.json.template
46+
[{
47+
"directory": "DIR",
48+
"command": "clang -S -emit-llvm DIR/tu.c -o DIR/tu.ll -include-pch DIR/prefix.pch -target x86_64-apple-macos12 -gmodules -g -Xclang -finclude-tree-preserve-pch-path",
49+
"file": "DIR/tu.c"
50+
}]
51+
52+
//--- cdb_no_preserve.json.template
53+
[{
54+
"directory": "DIR",
55+
"command": "clang -S -emit-llvm DIR/tu.c -o DIR/tu.ll -include-pch DIR/prefix.pch -target x86_64-apple-macos12 -gmodules -g",
56+
"file": "DIR/tu.c"
57+
}]
58+
59+
//--- prefix.h
60+
struct S {};
61+
62+
//--- tu.c
63+
struct S s;

clang/test/ClangScanDeps/modules-include-tree-missing-submodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
// CHECK-NOT: Bar
4848

4949
// CHECK-LABEL: TRANSLATION UNIT
50-
// CHECK: (PCH) llvmcas://
50+
// CHECK: (PCH) <PCH> llvmcas://
5151
// CHECK: [[PREFIX]]/tu.c llvmcas://
5252
// CHECK: 1:1 <built-in> llvmcas://
5353
// CHECK: 2:1 (Spurious import) (Module) Foo.Bar [[PREFIX]]/Foo.framework/Headers/Bar.h llvmcas://

clang/test/ClangScanDeps/modules-include-tree-pch-with-private.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
// CHECK: 3:1 (Module) Mod_Private
5454

5555
// CHECK-LABEL: TRANSLATION UNIT
56-
// CHECK: (PCH) llvmcas://
56+
// CHECK: (PCH) <PCH> llvmcas://
5757
// CHECK: [[PREFIX]]/tu.m llvmcas://
5858
// CHECK: 1:1 <built-in> llvmcas://
5959
// CHECK: 2:1 (Module) Mod_Private

0 commit comments

Comments
 (0)