Fix bugs when an included file name is typo corrected.
D52774 fixed a bug with typo correction of includes, but didn't add
a test.
D65907 then broke recovery of typo correction of includes again,
because it extracted the code that writes to Filename to a separate
function that took the parameter not by reference.
Fix that, and also don't repeat the slash normalization computation
and fix both lookup and regular file name after recovery.
Differential Revision: https://reviews.llvm.org/D79595
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 84c13b2..db24912 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -1716,11 +1716,11 @@
}
Optional<FileEntryRef> Preprocessor::LookupHeaderIncludeOrImport(
- const DirectoryLookup *&CurDir, StringRef Filename,
+ const DirectoryLookup *&CurDir, StringRef& Filename,
SourceLocation FilenameLoc, CharSourceRange FilenameRange,
const Token &FilenameTok, bool &IsFrameworkFound, bool IsImportDecl,
bool &IsMapped, const DirectoryLookup *LookupFrom,
- const FileEntry *LookupFromFile, StringRef LookupFilename,
+ const FileEntry *LookupFromFile, StringRef& LookupFilename,
SmallVectorImpl<char> &RelativePath, SmallVectorImpl<char> &SearchPath,
ModuleMap::KnownHeader &SuggestedModule, bool isAngled) {
Optional<FileEntryRef> File = LookupFile(
@@ -1789,21 +1789,10 @@
return Filename;
};
StringRef TypoCorrectionName = CorrectTypoFilename(Filename);
-
-#ifndef _WIN32
- // Normalize slashes when compiling with -fms-extensions on non-Windows.
- // This is unnecessary on Windows since the filesystem there handles
- // backslashes.
- SmallString<128> NormalizedTypoCorrectionPath;
- if (LangOpts.MicrosoftExt) {
- NormalizedTypoCorrectionPath = TypoCorrectionName;
- llvm::sys::path::native(NormalizedTypoCorrectionPath);
- TypoCorrectionName = NormalizedTypoCorrectionPath;
- }
-#endif
+ StringRef TypoCorrectionLookupName = CorrectTypoFilename(LookupFilename);
Optional<FileEntryRef> File = LookupFile(
- FilenameLoc, TypoCorrectionName, isAngled, LookupFrom, LookupFromFile,
+ FilenameLoc, TypoCorrectionLookupName, isAngled, LookupFrom, LookupFromFile,
CurDir, Callbacks ? &SearchPath : nullptr,
Callbacks ? &RelativePath : nullptr, &SuggestedModule, &IsMapped,
/*IsFrameworkFound=*/nullptr);
@@ -1818,6 +1807,7 @@
// We found the file, so set the Filename to the name after typo
// correction.
Filename = TypoCorrectionName;
+ LookupFilename = TypoCorrectionLookupName;
return File;
}
}
@@ -2074,8 +2064,6 @@
if (Callbacks && !IsImportDecl) {
// Notify the callback object that we've seen an inclusion directive.
// FIXME: Use a different callback for a pp-import?
- // FIXME: Passes wrong filename if LookupHeaderIncludeOrImport() did
- // typo correction.
Callbacks->InclusionDirective(
HashLoc, IncludeTok, LookupFilename, isAngled, FilenameRange,
File ? &File->getFileEntry() : nullptr, SearchPath, RelativePath,
@@ -2102,7 +2090,6 @@
!IsMapped && !File->getFileEntry().tryGetRealPathName().empty();
if (CheckIncludePathPortability) {
- // FIXME: Looks at the wrong filename if we did typo correction.
StringRef Name = LookupFilename;
StringRef NameWithoriginalSlashes = Filename;
#if defined(_WIN32)