Skip to content

[clang][modules] Stop eagerly reading files with diagnostic pragmas #8534

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
Apr 3, 2024
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
2 changes: 1 addition & 1 deletion clang/include/clang/Serialization/ASTBitCodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace serialization {
/// Version 4 of AST files also requires that the version control branch and
/// revision match exactly, since there is no backward compatibility of
/// AST files at this time.
const unsigned VERSION_MAJOR = 29;
const unsigned VERSION_MAJOR = 30;

/// AST file minor version number supported by this version of
/// Clang.
Expand Down
8 changes: 3 additions & 5 deletions clang/lib/Serialization/ASTReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6604,17 +6604,15 @@ void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
while (NumLocations--) {
assert(Idx < Record.size() &&
"Invalid data, missing pragma diagnostic states");
SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
assert(IDAndOffset.second == 0 && "not a start location for a FileID");
FileID FID = ReadFileID(F, Record, Idx);
assert(FID.isValid() && "invalid FileID for transition");
unsigned Transitions = Record[Idx++];

// Note that we don't need to set up Parent/ParentOffset here, because
// we won't be changing the diagnostic state within imported FileIDs
// (other than perhaps appending to the main source file, which has no
// parent).
auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
auto &F = Diag.DiagStatesByLoc.Files[FID];
F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
for (unsigned I = 0; I != Transitions; ++I) {
unsigned Offset = Record[Idx++];
Expand Down
4 changes: 1 addition & 3 deletions clang/lib/Serialization/ASTWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3131,9 +3131,7 @@ void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
continue;
++NumLocations;

SourceLocation Loc = Diag.SourceMgr->getComposedLoc(FileIDAndFile.first, 0);
assert(!Loc.isInvalid() && "start loc for valid FileID is invalid");
AddSourceLocation(Loc, Record);
AddFileID(FileIDAndFile.first, Record);

Record.push_back(FileIDAndFile.second.StateTransitions.size());
for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
Expand Down
34 changes: 34 additions & 0 deletions clang/test/Modules/home-is-cwd-search-paths.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// This test demonstrates how -fmodule-map-file-home-is-cwd with -fmodules-embed-all-files
// extend the importer search paths by relying on the side effects of pragma diagnostic
// mappings deserialization.

// RUN: rm -rf %t
// RUN: split-file %s %t

//--- dir1/a.modulemap
module a { header "a.h" }
//--- dir1/a.h
#include "search.h"
// The first compilation is configured such that -I search does contain the search.h header.
//--- dir1/search/search.h
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wparentheses"
#pragma clang diagnostic pop
// RUN: cd %t/dir1 && %clang_cc1 -fmodules -I search \
// RUN: -emit-module -fmodule-name=a a.modulemap -o %t/a.pcm \
// RUN: -fmodules-embed-all-files -fmodule-map-file-home-is-cwd

//--- dir2/b.modulemap
module b { header "b.h" }
//--- dir2/b.h
#include "search.h" // expected-error{{'search.h' file not found}}
// The second compilation is configured such that -I search is an empty directory.
// However, since b.pcm simply embeds the headers as "search/search.h", this compilation
// ends up seeing it too. This relies solely on ASTReader::ReadPragmaDiagnosticMappings()
// eagerly reading the corresponding INPUT_FILE record before header search happens.
// Removing the eager deserialization makes this header invisible and so does removing
// the pragma directives.
// RUN: mkdir %t/dir2/search
// RUN: cd %t/dir2 && %clang_cc1 -fmodules -I search \
// RUN: -emit-module -fmodule-name=b b.modulemap -o %t/b.pcm \
// RUN: -fmodule-file=%t/a.pcm -verify