Skip to content

[clang][modules] Cherry-picks for minor fixes #10854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 18, 2025
Merged
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
5 changes: 3 additions & 2 deletions clang/include/clang/Basic/DiagnosticSerializationKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ def err_fe_ast_file_modified : Error<
DefaultFatal;
def err_fe_pch_file_overridden : Error<
"file '%0' from the precompiled header has been overridden">;
def note_pch_required_by : Note<"'%0' required by '%1'">;
def note_pch_rebuild_required : Note<"please rebuild precompiled header '%0'">;
def note_ast_file_required_by : Note<"'%0' required by '%1'">;
def note_ast_file_rebuild_required
: Note<"please rebuild precompiled file '%0'">;
def note_module_cache_path : Note<
"after modifying system headers, please delete the module cache at '%0'">;

Expand Down
9 changes: 9 additions & 0 deletions clang/lib/Lex/DependencyDirectivesScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,15 @@ bool Scanner::lexModuleDirectiveBody(DirectiveKind Kind, const char *&First,
const char *const End) {
const char *DirectiveLoc = Input.data() + CurDirToks.front().Offset;
for (;;) {
// Keep a copy of the First char incase it needs to be reset.
const char *Previous = First;
const dependency_directives_scan::Token &Tok = lexToken(First, End);
if ((Tok.is(tok::hash) || Tok.is(tok::at)) &&
(Tok.Flags & clang::Token::StartOfLine)) {
CurDirToks.pop_back();
First = Previous;
return false;
}
if (Tok.is(tok::eof))
return reportError(
DirectiveLoc,
Expand Down Expand Up @@ -846,6 +854,7 @@ bool Scanner::lexPPLine(const char *&First, const char *const End) {
if (*First == '@')
return lexAt(First, End);

// Handle module directives for C++20 modules.
if (*First == 'i' || *First == 'e' || *First == 'm')
return lexModule(First, End);

Expand Down
14 changes: 7 additions & 7 deletions clang/lib/Serialization/ASTReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2680,25 +2680,25 @@ InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
while (!ImportStack.back()->ImportedBy.empty())
ImportStack.push_back(ImportStack.back()->ImportedBy[0]);

// The top-level PCH is stale.
StringRef TopLevelPCHName(ImportStack.back()->FileName);
// The top-level AST file is stale.
StringRef TopLevelASTFileName(ImportStack.back()->FileName);
Diag(diag::err_fe_ast_file_modified)
<< *Filename << moduleKindForDiagnostic(ImportStack.back()->Kind)
<< TopLevelPCHName << FileChange.Kind
<< TopLevelASTFileName << FileChange.Kind
<< (FileChange.Old && FileChange.New)
<< llvm::itostr(FileChange.Old.value_or(0))
<< llvm::itostr(FileChange.New.value_or(0));

// Print the import stack.
if (ImportStack.size() > 1) {
Diag(diag::note_pch_required_by)
Diag(diag::note_ast_file_required_by)
<< *Filename << ImportStack[0]->FileName;
for (unsigned I = 1; I < ImportStack.size(); ++I)
Diag(diag::note_pch_required_by)
<< ImportStack[I-1]->FileName << ImportStack[I]->FileName;
Diag(diag::note_ast_file_required_by)
<< ImportStack[I - 1]->FileName << ImportStack[I]->FileName;
}

Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
Diag(diag::note_ast_file_rebuild_required) << TopLevelASTFileName;
}

IsOutOfDate = true;
Expand Down
1 change: 1 addition & 0 deletions clang/test/Modules/module-file-modified.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
#include "a.h"
int foo = 0; // redefinition of 'foo'
// CHECK: fatal error: file {{.*}} has been modified since the module file {{.*}} was built
// CHECK: note: please rebuild precompiled file
// REQUIRES: shell
2 changes: 1 addition & 1 deletion clang/test/Modules/validate-file-content.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
// CHECK: file '[[M_H:.*[/\\]m\.h]]' has been modified since the precompiled header '[[A_PCH:.*/a\.pch]]' was built: content changed
// CHECK: '[[M_H]]' required by '[[M_PCM:.*[/\\]m.*\.pcm]]'
// CHECK: '[[M_PCM]]' required by '[[A_PCH]]'
// CHECK: please rebuild precompiled header '[[A_PCH]]'
// CHECK: please rebuild precompiled file '[[A_PCH]]'
// expected-no-diagnostics
2 changes: 1 addition & 1 deletion clang/test/PCH/modified-module-dependency.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
// CHECK: file '[[TEST_H:.*[/\\]test\.h]]' has been modified since the precompiled header '[[PREFIX_PCH:.*/prefix\.pch]]' was built
// CHECK: '[[TEST_H]]' required by '[[TEST_PCM:.*[/\\]test\.pcm]]'
// CHECK: '[[TEST_PCM]]' required by '[[PREFIX_PCH]]'
// CHECK: please rebuild precompiled header '[[PREFIX_PCH]]'
// CHECK: please rebuild precompiled file '[[PREFIX_PCH]]'
2 changes: 1 addition & 1 deletion clang/test/PCH/validate-file-content.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
// RUN: FileCheck %s < %t/stderr
//
// CHECK: file '[[M_H:.*[/\\]m\.h]]' has been modified since the precompiled header '[[A_PCH:.*/a\.pch]]' was built: content changed
// CHECK: please rebuild precompiled header '[[A_PCH]]'
// CHECK: please rebuild precompiled file '[[A_PCH]]'
// expected-no-diagnostics
17 changes: 16 additions & 1 deletion clang/unittests/Lex/DependencyDirectivesScannerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -661,13 +661,28 @@ TEST(MinimizeSourceToDependencyDirectivesTest, EmptyIncludesAndImports) {
Out));
}

TEST(MinimizeSourceToDependencyDirectivesTest, AtImportFailures) {
TEST(MinimizeSourceToDependencyDirectivesTest, ImportFailures) {
SmallVector<char, 128> Out;

ASSERT_TRUE(minimizeSourceToDependencyDirectives("@import A\n", Out));
ASSERT_FALSE(
minimizeSourceToDependencyDirectives("@import MACRO(A);\n", Out));
ASSERT_FALSE(minimizeSourceToDependencyDirectives("@import \" \";\n", Out));

ASSERT_FALSE(minimizeSourceToDependencyDirectives("import <Foo.h>\n"
"@import Foo;",
Out));
EXPECT_STREQ("@import Foo;\n", Out.data());

ASSERT_FALSE(
minimizeSourceToDependencyDirectives("import <Foo.h>\n"
"#import <Foo.h>\n"
"@;\n"
"#pragma clang module import Foo",
Out));
EXPECT_STREQ("#import <Foo.h>\n"
"#pragma clang module import Foo\n",
Out.data());
}

TEST(MinimizeSourceToDependencyDirectivesTest, RawStringLiteral) {
Expand Down