Skip to content
Closed
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: 2 additions & 1 deletion clang/lib/Frontend/FrontendActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ void VerifyPCHAction::ExecuteAction() {
DisableValidationForModuleKind::None,
/*AllowASTWithCompilerErrors*/ false,
/*AllowConfigurationMismatch*/ true,
/*ValidateSystemInputs*/ true));
/*ValidateSystemInputs*/ true,
CI.getHeaderSearchOpts().ValidateASTInputFilesContent));

Reader->ReadAST(getCurrentFile(),
Preamble ? serialization::MK_Preamble
Expand Down
18 changes: 18 additions & 0 deletions clang/lib/Serialization/ASTWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4781,6 +4781,24 @@ bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Changed = true;
}

// If we are generating a normal PCH (EG. not a C++ module).
if (!WritingModule) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that without this guard the following tests would fail:

Failed Tests (2):
  Clang :: ClangScanDeps/modules-file-name-as-requested.m
  Clang :: ClangScanDeps/modules-implementation-vfs.m

// Use the vfs overlay if it exists to translate paths.
auto &FileSys =
Context->getSourceManager().getFileManager().getVirtualFileSystem();
if (auto *RFS = dyn_cast<llvm::vfs::RedirectingFileSystem>(&FileSys)) {
if (auto Result = RFS->lookupPath(StringRef(Path.data(), Path.size()))) {
if (std::optional<StringRef> Redirect = Result->getExternalRedirect()) {
const char *data = Redirect->data();
const size_t size = Redirect->size();
Path.resize(size);
Path.assign(data, data + size);
Changed = true;
}
}
}
}

return Changed;
}

Expand Down
8 changes: 8 additions & 0 deletions clang/test/PCH/verify_no_timestamp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: echo 'int SomeFunc() { return 42; }' > %t.h
// RUN: %clang_cc1 -Werror -fno-pch-timestamp -fvalidate-ast-input-files-content -emit-pch -o "%t.pch" %t.h

// Now change the source file, which should cause the verifier to fail with content mismatch.
// RUN: echo 'int SomeFunc() { return 13; }' > %t.h
// RUN: not %clang_cc1 -fno-pch-timestamp -fvalidate-ast-input-files-content -verify-pch %t.pch 2>&1 | FileCheck %s -DT=%t

// CHECK: fatal error: file '[[T]].h' has been modified since the precompiled header '[[T]].pch' was built: content changed
9 changes: 9 additions & 0 deletions clang/test/VFS/Inputs/vfsoverlay-directory-remap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
'version': 0,
'roots': [
{ 'name': 'FROM_DIR',
'type': 'directory-remap',
'external-contents': 'TO_DIR'
}
]
}
27 changes: 27 additions & 0 deletions clang/test/VFS/remap-to-fake.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// RUN: split-file %s %t
// RUN: sed -e "s@FROM_DIR@%{/t:regex_replacement}/From@g" -e "s@TO_DIR@%{/t:regex_replacement}/Fake@g" %S/Inputs/vfsoverlay-directory-remap.yaml > %t/to-fake.yaml
// RUN: sed -e "s@FROM_DIR@%{/t:regex_replacement}/Fake@g" -e "s@TO_DIR@%{/t:regex_replacement}/To@g" %S/Inputs/vfsoverlay-directory-remap.yaml > %t/from-fake.yaml

// RUN: %clang_cc1 -Werror -fno-pch-timestamp -fvalidate-ast-input-files-content -ivfsoverlay %t/to-fake.yaml -emit-pch -o "%t.pch" %t/From/../From/B.h

// Remove the `From` directory as we don't want to accidentally find that source if the PCH hasn't remapped using the VFS!
// RUN: rm -rf %t/From

// The PCH will be invalid because the `Fake` directory does not exist.
// RUN: not %clang_cc1 -fno-pch-timestamp -fvalidate-ast-input-files-content -verify-pch %t.pch

// But if we specify the correct VFS overlay it'll verify clean.
// RUN: %clang_cc1 -fno-pch-timestamp -fvalidate-ast-input-files-content -verify-pch -ivfsoverlay %t/from-fake.yaml %t.pch

// RUN: %clang_cc1 -fno-pch-timestamp -fvalidate-ast-input-files-content -Werror -I %t/To -ivfsoverlay %t/from-fake.yaml -include-pch "%t.pch" -emit-llvm -C %t/Test.c -o %t.o

//--- From/B.h
#pragma once
int SomeFunc() { return 13; }
//--- To/B.h
#pragma once
int SomeFunc() { return 13; }
//--- Test.c
#include "B.h"

int UseSomeFunc() { return SomeFunc(); }