[PATCH] D52774: [Preprocessor] Fix an assertion failure trigged in clangd #include completion.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 2 03:01:05 PDT 2018


hokein created this revision.
hokein added a reviewer: sammccall.
Herald added subscribers: kadircet, arphaman, jkorous, MaskRay, ioeric, ilya-biryukov.

- Fix an assertion, the Filename should fallback to the written name in

the code if the correct typo heuristic fails.

- Fix an inconsistent issue of NormalizedPath and Filename, NormalizedPath should

always correspond to the Filename. Previously, NormalizedPath was the old one if we find a
file after typo correction.

I checked the Filename usage after the typo correct code, it seems expected.
The "check-clang" test is passed, so I believe this change doesn't introduce
any unexpected functional change.

The test is added in extra repo. I tried to add the test to clang's code
completion test, it doesn't reporduce the crash like clangd.


Repository:
  rC Clang

https://reviews.llvm.org/D52774

Files:
  lib/Lex/PPDirectives.cpp


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;
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52774.167907.patch
Type: text/x-patch
Size: 3511 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181002/41e2b23b/attachment-0001.bin>


More information about the cfe-commits mailing list