Skip to content

Commit 1dd2f86

Browse files
committed
Address review feedback
* Fix logic error when deserializing input file * Also add a check for absolute paths in `isPathInStableDir` * Always passdown filenameAsRequested & externalfilename
1 parent 3aaaf33 commit 1dd2f86

File tree

6 files changed

+51
-25
lines changed

6 files changed

+51
-25
lines changed

clang/include/clang/Serialization/ASTReader.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,9 @@ class ASTReaderListener {
242242
/// paths, for example when deserializing input files from AST files.
243243
///
244244
/// \returns true to continue receiving the next input file, false to stop.
245-
virtual bool visitInputFile(StringRef FilenameAsRequested, StringRef Filename,
246-
bool isSystem, bool isOverridden,
247-
bool isExplicitModule) {
245+
virtual bool visitInputFile(StringRef FilenameAsRequested,
246+
StringRef ExternalFilename, bool isSystem,
247+
bool isOverridden, bool isExplicitModule) {
248248
return true;
249249
}
250250

clang/lib/Frontend/FrontendActions.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -777,9 +777,9 @@ namespace {
777777
/// Indicates that the AST file contains particular input file.
778778
///
779779
/// \returns true to continue receiving the next input file, false to stop.
780-
bool visitInputFile(StringRef FilenameAsRequested, StringRef Filename,
781-
bool isSystem, bool isOverridden,
782-
bool isExplicitModule) override {
780+
bool visitInputFile(StringRef FilenameAsRequested,
781+
StringRef ExternalFilename, bool isSystem,
782+
bool isOverridden, bool isExplicitModule) override {
783783

784784
Out.indent(2) << "Input file: " << FilenameAsRequested;
785785

clang/lib/Serialization/ASTReader.cpp

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2616,6 +2616,18 @@ bool ASTReader::shouldDisableValidationForFile(
26162616
return false;
26172617
}
26182618

2619+
namespace {
2620+
2621+
std::pair<StringRef, StringRef>
2622+
getUnresolvedInputFilenames(const ASTReader::RecordData &Record,
2623+
const StringRef InputBlob) {
2624+
uint16_t AsRequestedLength = Record[7];
2625+
return {InputBlob.substr(0, AsRequestedLength),
2626+
InputBlob.substr(AsRequestedLength)};
2627+
}
2628+
2629+
} // namespace
2630+
26192631
InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {
26202632
// If this ID is bogus, just return an empty input file.
26212633
if (ID == 0 || ID > F.InputFileInfosLoaded.size())
@@ -2659,11 +2671,12 @@ InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {
26592671
R.Transient = static_cast<bool>(Record[4]);
26602672
R.TopLevel = static_cast<bool>(Record[5]);
26612673
R.ModuleMap = static_cast<bool>(Record[6]);
2662-
uint16_t AsRequestedLength = Record[7];
2663-
R.UnresolvedImportedFilenameAsRequested = Blob.substr(0, AsRequestedLength);
2664-
R.UnresolvedImportedFilename = Blob.substr(AsRequestedLength);
2665-
if (R.UnresolvedImportedFilename.empty())
2666-
R.UnresolvedImportedFilename = R.UnresolvedImportedFilenameAsRequested;
2674+
auto [UnresolvedFilenameAsRequested, UnresolvedFilename] =
2675+
getUnresolvedInputFilenames(Record, Blob);
2676+
R.UnresolvedImportedFilenameAsRequested = UnresolvedFilenameAsRequested;
2677+
R.UnresolvedImportedFilename = UnresolvedFilename.empty()
2678+
? UnresolvedFilenameAsRequested
2679+
: UnresolvedFilename;
26672680

26682681
Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
26692682
if (!MaybeEntry) // FIXME this drops errors on the floor.
@@ -5704,6 +5717,11 @@ bool ASTReader::readASTFileControlBlock(
57045717
bool DoneWithControlBlock = false;
57055718
SmallString<0> PathBuf;
57065719
PathBuf.reserve(256);
5720+
// Additional path buffer to use when multiple paths need to be resolved.
5721+
// For example, when deserializing input files that contains a path that was
5722+
// resolved from a vfs overlay and an external location.
5723+
SmallString<0> AdditionalPathBuf;
5724+
AdditionalPathBuf.reserve(256);
57075725
while (!DoneWithControlBlock) {
57085726
Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
57095727
if (!MaybeEntry) {
@@ -5835,12 +5853,20 @@ bool ASTReader::readASTFileControlBlock(
58355853
break;
58365854
case INPUT_FILE:
58375855
bool Overridden = static_cast<bool>(Record[3]);
5838-
size_t FilenameLen = ModuleDir.size() + Record[7] + 1;
5839-
auto Filename = ResolveImportedPath(PathBuf, Blob, ModuleDir);
5840-
StringRef FilenameAsRequested = Filename->substr(0, FilenameLen);
5841-
StringRef ExternalFilename = Filename->substr(FilenameLen);
5856+
auto [UnresolvedFilenameAsRequested, UnresolvedFilename] =
5857+
getUnresolvedInputFilenames(Record, Blob);
5858+
auto FilenameAsRequestedBuf = ResolveImportedPath(
5859+
PathBuf, UnresolvedFilenameAsRequested, ModuleDir);
5860+
StringRef Filename;
5861+
if (UnresolvedFilename.empty())
5862+
Filename = *FilenameAsRequestedBuf;
5863+
else {
5864+
auto FilenameBuf = ResolveImportedPath(
5865+
AdditionalPathBuf, UnresolvedFilename, ModuleDir);
5866+
Filename = *FilenameBuf;
5867+
}
58425868
shouldContinue = Listener.visitInputFile(
5843-
FilenameAsRequested, ExternalFilename, isSystemFile, Overridden,
5869+
*FilenameAsRequestedBuf, Filename, isSystemFile, Overridden,
58445870
/*IsExplicitModule=*/false);
58455871
break;
58465872
}

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,8 @@ class PrebuiltModuleListener : public ASTReaderListener {
136136
!PrebuiltModulesASTMap[CurrentFile].isInStableDir())
137137
return false;
138138

139-
const StringRef FileToUse =
140-
ExternalFilename.empty() ? FilenameAsRequested : ExternalFilename;
141-
142139
PrebuiltModulesASTMap[CurrentFile].setInStableDir(
143-
isPathInStableDir(StableDirs, FileToUse));
140+
isPathInStableDir(StableDirs, ExternalFilename));
144141
return PrebuiltModulesASTMap[CurrentFile].isInStableDir();
145142
}
146143

clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,14 @@ void dependencies::resetBenignCodeGenOptions(frontend::ActionKind ProgramAction,
228228

229229
bool dependencies::isPathInStableDir(const ArrayRef<StringRef> Directories,
230230
const StringRef Input) {
231+
using namespace llvm::sys;
232+
233+
if (!path::is_absolute(Input))
234+
return false;
235+
231236
auto PathStartsWith = [](StringRef Prefix, StringRef Path) {
232-
auto PrefixIt = llvm::sys::path::begin(Prefix),
233-
PrefixEnd = llvm::sys::path::end(Prefix);
234-
for (auto PathIt = llvm::sys::path::begin(Path),
235-
PathEnd = llvm::sys::path::end(Path);
237+
auto PrefixIt = path::begin(Prefix), PrefixEnd = path::end(Prefix);
238+
for (auto PathIt = path::begin(Path), PathEnd = path::end(Path);
236239
PrefixIt != PrefixEnd && PathIt != PathEnd; ++PrefixIt, ++PathIt) {
237240
if (*PrefixIt != *PathIt)
238241
return false;

clang/test/ClangScanDeps/prebuilt-modules-in-stable-dirs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/// resolve `is-in-stable-directories` correctly.
33
/// The steps are:
44
/// 1. Scan dependencies to build the PCH. One of the module's depend on header
5-
/// that is seemingly from the sysroot. However, it depends on a local header that is overlaid..
5+
/// that is seemingly from the sysroot. However, it depends on a local header that is overlaid.
66
/// 2. Build the PCH & dependency PCMs.
77
/// 3. Scan a source file that transitively depends on the same modules as the pcm.
88

0 commit comments

Comments
 (0)