Index: lib/Lex/PPDirectives.cpp =================================================================== --- lib/Lex/PPDirectives.cpp +++ lib/Lex/PPDirectives.cpp @@ -1833,13 +1833,18 @@ // the path. ModuleMap::KnownHeader SuggestedModule; SourceLocation FilenameLoc = FilenameTok.getLocation(); - SmallString<128> NormalizedPath; - if (LangOpts.MSVCCompat) { - NormalizedPath = Filename.str(); + auto NormalizePathForMSVCCompat = [this](llvm::StringRef FilePath) { + SmallString<128> NormalizedPath; + if (!LangOpts.MSVCCompat) + return NormalizedPath; + NormalizedPath = FilePath; #ifndef _WIN32 llvm::sys::path::native(NormalizedPath); #endif - } + return NormalizedPath; + }; + SmallString<128> NormalizedPath = NormalizePathForMSVCCompat(Filename); + const FileEntry *File = LookupFile( FilenameLoc, LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled, LookupFrom, LookupFromFile, CurDir, @@ -1887,7 +1892,6 @@ // Check for likely typos due to leading or trailing non-isAlphanumeric // characters - StringRef OriginalFilename = Filename; if (!File) { // A heuristic to correct a typo file name by removing leading and // trailing non-isAlphanumeric characters. @@ -1898,27 +1902,35 @@ } return Filename; }; - Filename = CorrectTypoFilename(Filename); + StringRef TypoCorrectionName = CorrectTypoFilename(Filename); + auto NormalizedTypoCorrectionName = + NormalizePathForMSVCCompat(TypoCorrectionName); File = LookupFile( FilenameLoc, - LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled, - LookupFrom, LookupFromFile, CurDir, + LangOpts.MSVCCompat ? NormalizedTypoCorrectionName.c_str() + : TypoCorrectionName, + isAngled, LookupFrom, LookupFromFile, CurDir, Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr, &SuggestedModule, &IsMapped); if (File) { SourceRange Range(FilenameTok.getLocation(), CharEnd); - auto Hint = isAngled ? FixItHint::CreateReplacement( - Range, "<" + Filename.str() + ">") - : FixItHint::CreateReplacement( - Range, "\"" + Filename.str() + "\""); + auto Hint = isAngled + ? FixItHint::CreateReplacement( + Range, "<" + TypoCorrectionName.str() + ">") + : FixItHint::CreateReplacement( + Range, "\"" + TypoCorrectionName.str() + "\""); Diag(FilenameTok, diag::err_pp_file_not_found_typo_not_fatal) - << OriginalFilename << Filename << Hint; + << Filename << TypoCorrectionName << Hint; + // We found the file, so set the Filename and NormalizedPath to the + // name after typo correction. + Filename = TypoCorrectionName; + NormalizedPath = NormalizedTypoCorrectionName; } } // If the file is still not found, just go with the vanilla diagnostic if (!File) - Diag(FilenameTok, diag::err_pp_file_not_found) << OriginalFilename + Diag(FilenameTok, diag::err_pp_file_not_found) << Filename << FilenameRange; } }