hokein created this revision.
hokein added a reviewer: sammccall.
Herald added a subscriber: jsji.

This also fixes a crash of code completion on `#include "./^"`.


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,27 +1889,29 @@
       // characters
       StringRef OriginalFilename = Filename;
       if (!File) {
-        while (!isAlphanumeric(Filename.front())) {
+        while (!Filename.empty() && !isAlphanumeric(Filename.front())) {
           Filename = Filename.drop_front();
         }
-        while (!isAlphanumeric(Filename.back())) {
+        while (!Filename.empty() && !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);
-        if (File) {
-          SourceRange Range(FilenameTok.getLocation(), CharEnd);
-          auto Hint = isAngled ? FixItHint::CreateReplacement(
-                                     Range, "<" + Filename.str() + ">")
-                               : FixItHint::CreateReplacement(
-                                     Range, "\"" + Filename.str() + "\"");
-          Diag(FilenameTok, diag::err_pp_file_not_found_typo_not_fatal)
-              << OriginalFilename << Filename << Hint;
+        if (!Filename.empty()) {
+          File = 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(
+                                      Range, "<" + Filename.str() + ">")
+                                : FixItHint::CreateReplacement(
+                                      Range, "\"" + Filename.str() + "\"");
+            Diag(FilenameTok, diag::err_pp_file_not_found_typo_not_fatal)
+                << OriginalFilename << Filename << Hint;
+          }
         }
       }
 
@@ -1920,6 +1922,9 @@
     }
   }
 
+  if (Filename.empty())
+    return;
+
   if (usingPCHWithThroughHeader() && SkippingUntilPCHThroughHeader) {
     if (isPCHThroughHeader(File))
       SkippingUntilPCHThroughHeader = false;


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,27 +1889,29 @@
       // characters
       StringRef OriginalFilename = Filename;
       if (!File) {
-        while (!isAlphanumeric(Filename.front())) {
+        while (!Filename.empty() && !isAlphanumeric(Filename.front())) {
           Filename = Filename.drop_front();
         }
-        while (!isAlphanumeric(Filename.back())) {
+        while (!Filename.empty() && !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);
-        if (File) {
-          SourceRange Range(FilenameTok.getLocation(), CharEnd);
-          auto Hint = isAngled ? FixItHint::CreateReplacement(
-                                     Range, "<" + Filename.str() + ">")
-                               : FixItHint::CreateReplacement(
-                                     Range, "\"" + Filename.str() + "\"");
-          Diag(FilenameTok, diag::err_pp_file_not_found_typo_not_fatal)
-              << OriginalFilename << Filename << Hint;
+        if (!Filename.empty()) {
+          File = 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(
+                                      Range, "<" + Filename.str() + ">")
+                                : FixItHint::CreateReplacement(
+                                      Range, "\"" + Filename.str() + "\"");
+            Diag(FilenameTok, diag::err_pp_file_not_found_typo_not_fatal)
+                << OriginalFilename << Filename << Hint;
+          }
         }
       }
 
@@ -1920,6 +1922,9 @@
     }
   }
 
+  if (Filename.empty())
+    return;
+
   if (usingPCHWithThroughHeader() && SkippingUntilPCHThroughHeader) {
     if (isPCHThroughHeader(File))
       SkippingUntilPCHThroughHeader = false;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to