[PATCH] D52721: [Preprocessor] Fix a crash when handling non-alpha include header.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 1 06:50:24 PDT 2018


hokein updated this revision to Diff 167724.
hokein marked an inline comment as done.
hokein added a comment.

Update the code.


Repository:
  rC Clang

https://reviews.llvm.org/D52721

Files:
  lib/Lex/PPDirectives.cpp
  test/Preprocessor/include-nonalpha-no-crash.c


Index: test/Preprocessor/include-nonalpha-no-crash.c
===================================================================
--- /dev/null
+++ test/Preprocessor/include-nonalpha-no-crash.c
@@ -0,0 +1,3 @@
+// RUN: %clang_cc1 %s -verify
+
+#include "./" // expected-error {{'./' file not found}}
Index: lib/Lex/PPDirectives.cpp
===================================================================
--- lib/Lex/PPDirectives.cpp
+++ lib/Lex/PPDirectives.cpp
@@ -1889,19 +1889,31 @@
       // characters
       StringRef OriginalFilename = Filename;
       if (!File) {
-        while (!isAlphanumeric(Filename.front())) {
-          Filename = Filename.drop_front();
-        }
-        while (!isAlphanumeric(Filename.back())) {
-          Filename = Filename.drop_back();
-        }
-
-        File = LookupFile(
-            FilenameLoc,
-            LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
-            LookupFrom, LookupFromFile, CurDir,
-            Callbacks ? &SearchPath : nullptr,
-            Callbacks ? &RelativePath : nullptr, &SuggestedModule, &IsMapped);
+        // A heuristic to correct a typo file name by removing leading and
+        // trailing non-isAlphanumeric characters.
+        auto CorrectTypoFilename = [](llvm::StringRef Filename) {
+          while (!Filename.empty() && !isAlphanumeric(Filename.front())) {
+            Filename = Filename.drop_front();
+          }
+          if (Filename.empty())
+            return Filename;
+          while (!isAlphanumeric(Filename.back())) {
+            Filename = Filename.drop_back();
+            assert(!Filename.empty());
+          }
+          return Filename;
+        };
+        Filename = CorrectTypoFilename(Filename);
+
+        File = Filename.empty()
+                   ? nullptr
+                   : LookupFile(FilenameLoc,
+                                LangOpts.MSVCCompat ? NormalizedPath.c_str()
+                                                    : Filename,
+                                isAngled, LookupFrom, LookupFromFile, CurDir,
+                                Callbacks ? &SearchPath : nullptr,
+                                Callbacks ? &RelativePath : nullptr,
+                                &SuggestedModule, &IsMapped);
         if (File) {
           SourceRange Range(FilenameTok.getLocation(), CharEnd);
           auto Hint = isAngled ? FixItHint::CreateReplacement(


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52721.167724.patch
Type: text/x-patch
Size: 2412 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181001/cf7257de/attachment.bin>


More information about the cfe-commits mailing list