]> granicus.if.org Git - clang/commitdiff
Adding a fixit for includes that cannot be found with angle brackets, but can be...
authorAaron Ballman <aaron@aaronballman.com>
Tue, 17 Jul 2012 23:19:16 +0000 (23:19 +0000)
committerAaron Ballman <aaron@aaronballman.com>
Tue, 17 Jul 2012 23:19:16 +0000 (23:19 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@160406 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticLexKinds.td
lib/Lex/PPDirectives.cpp
test/FixIt/fixit-include.c [new file with mode: 0644]
test/FixIt/fixit-include.h [new file with mode: 0644]

index 77b7f4287231aea1b251151fe1977efea78ca6af..1d55d6e50d44bd6eef5bcda9f8f6b3e3f401b168 100644 (file)
@@ -279,6 +279,8 @@ def note_macro_here : Note<"macro %0 defined here">;
 
 def err_pp_invalid_directive : Error<"invalid preprocessing directive">;
 def err_pp_file_not_found : Error<"'%0' file not found">, DefaultFatal;
+def err_pp_file_not_found_not_fatal : Error<
+  "'%0' file not found with <angled> include; use \"quotes\" instead">;
 def err_pp_error_opening_file : Error<
   "error opening file '%0': %1">, DefaultFatal;
 def err_pp_empty_filename : Error<"empty filename">;
index a6b7b52624ef8c0cc97263bc2da5c7111678a9b8..74b9cbc881ac0368dc81b1c9fcf511cab31a82a1 100644 (file)
@@ -1390,9 +1390,28 @@ void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
   }
   
   if (File == 0) {
-    if (!SuppressIncludeNotFoundError)
-      Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
-    return;
+    if (!SuppressIncludeNotFoundError) {
+      // If the file could not be located and it was included via angle 
+      // brackets, we can attempt a lookup as though it were a quoted path to
+      // provide the user with a possible fixit.
+      if (isAngled) {
+        File = LookupFile(Filename, false, LookupFrom, CurDir, 
+                          Callbacks ? &SearchPath : 0, 
+                          Callbacks ? &RelativePath : 0, 
+                          getLangOpts().Modules ? &SuggestedModule : 0);
+        if (File) {
+          SourceRange Range(FilenameTok.getLocation(), CharEnd);
+          Diag(FilenameTok, diag::err_pp_file_not_found_not_fatal) << 
+            Filename << 
+            FixItHint::CreateReplacement(Range, "\"" + Filename.str() + "\"");
+        }
+      }
+      // If the file is still not found, just go with the vanilla diagnostic
+      if (!File)
+        Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
+    }
+    if (!File)
+      return;
   }
 
   // If we are supposed to import a module rather than including the header,
diff --git a/test/FixIt/fixit-include.c b/test/FixIt/fixit-include.c
new file mode 100644 (file)
index 0000000..9da9409
--- /dev/null
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -fsyntax-only -Wall -pedantic -verify %s\r
+// RUN: cp %s %t\r
+// RUN: cp %S/fixit-include.h %T\r
+// RUN: not %clang_cc1 -fsyntax-only -fixit %t\r
+// RUN: %clang_cc1 -Wall -pedantic %t\r
+\r
+#include <fixit-include.h> // expected-error {{'fixit-include.h' file not found with <angled> include; use "quotes" instead}}\r
+\r
+#pragma does_not_exist // expected-warning {{unknown pragma ignored}}\r
+\r
+int main( void ) {\r
+  return 0;\r
+}\r
diff --git a/test/FixIt/fixit-include.h b/test/FixIt/fixit-include.h
new file mode 100644 (file)
index 0000000..6a22d2e
--- /dev/null
@@ -0,0 +1 @@
+// This file is purposefully left empty\r