christylee created this revision.
Herald added a subscriber: cfe-commits.

When someone writes

  #include "<some_file.h>"

or

  #include " some_file.h "

the compiler returns "file not fond..." with fonts and quotes that may make it 
hard to see there are excess quotes or surprising bytes in the filename.  This 
feature raises a warning about likely typo and prompts for expected include 
statement format when:
a). the code fails to compile and errors on "file not found", and
b). the first or last character of the filename detected is a non-alphanumeric 
character.

Assuming that files are usually logically named and start and end with an 
alphanumeric character, this feature should help shorten the time for future 
debugging due to simple typos.


Repository:
  rC Clang

https://reviews.llvm.org/D51333

Files:
  include/clang/Basic/DiagnosticLexKinds.td
  lib/Lex/PPDirectives.cpp
  test/Frontend/include-likely-typo.c


Index: test/Frontend/include-likely-typo.c
===================================================================
--- /dev/null
+++ test/Frontend/include-likely-typo.c
@@ -0,0 +1,4 @@
+// RUN: not %clang_cc1 -verify -frewrite-includes
+#include "<hello.h>" @expected-warning {{likely typo, expected "FILENAME" or 
<FILENAME> but filename is '<hello.h>'}}
+@expected-error {{'<hello.h>' file not found}}
+#include " hello.h " @expected-warning {{likely typo, expected "FILENAME" or 
<FILENAME> but filename is ' hello.h '}}
Index: lib/Lex/PPDirectives.cpp
===================================================================
--- lib/Lex/PPDirectives.cpp
+++ lib/Lex/PPDirectives.cpp
@@ -1875,9 +1875,16 @@
       }
 
       // If the file is still not found, just go with the vanilla diagnostic
-      if (!File)
-        Diag(FilenameTok, diag::err_pp_file_not_found) << Filename
-                                                       << FilenameRange;
+      if (!File) {
+        // Assuming filename logically starts and end with alphnumeric
+        // character
+        if (!isAlphanumeric(Filename.front()) ||
+            !isAlphanumeric(Filename.back())) {
+          Diag(FilenameTok, diag::warn_pp_filename_likely_typo) << Filename;
+        }
+        Diag(FilenameTok, diag::err_pp_file_not_found)
+            << Filename << FilenameRange;
+      }
     }
   }
 
Index: include/clang/Basic/DiagnosticLexKinds.td
===================================================================
--- include/clang/Basic/DiagnosticLexKinds.td
+++ include/clang/Basic/DiagnosticLexKinds.td
@@ -471,6 +471,10 @@
 def err_pp_identifier_arg_not_identifier : Error<
   "cannot convert %0 token to an identifier">;
 
+def warn_pp_filename_likely_typo :
+  ExtWarn<"likely typo, expected \"FILENAME\" or <FILENAME> "
+  "but filename is '%0'">, InGroup<UnknownPragmas>;
+
 def warn_pragma_include_alias_mismatch_angle :
    ExtWarn<"angle-bracketed include <%0> cannot be aliased to double-quoted "
    "include \"%1\"">, InGroup<UnknownPragmas>;


Index: test/Frontend/include-likely-typo.c
===================================================================
--- /dev/null
+++ test/Frontend/include-likely-typo.c
@@ -0,0 +1,4 @@
+// RUN: not %clang_cc1 -verify -frewrite-includes
+#include "<hello.h>" @expected-warning {{likely typo, expected "FILENAME" or <FILENAME> but filename is '<hello.h>'}}
+@expected-error {{'<hello.h>' file not found}}
+#include " hello.h " @expected-warning {{likely typo, expected "FILENAME" or <FILENAME> but filename is ' hello.h '}}
Index: lib/Lex/PPDirectives.cpp
===================================================================
--- lib/Lex/PPDirectives.cpp
+++ lib/Lex/PPDirectives.cpp
@@ -1875,9 +1875,16 @@
       }
 
       // If the file is still not found, just go with the vanilla diagnostic
-      if (!File)
-        Diag(FilenameTok, diag::err_pp_file_not_found) << Filename
-                                                       << FilenameRange;
+      if (!File) {
+        // Assuming filename logically starts and end with alphnumeric
+        // character
+        if (!isAlphanumeric(Filename.front()) ||
+            !isAlphanumeric(Filename.back())) {
+          Diag(FilenameTok, diag::warn_pp_filename_likely_typo) << Filename;
+        }
+        Diag(FilenameTok, diag::err_pp_file_not_found)
+            << Filename << FilenameRange;
+      }
     }
   }
 
Index: include/clang/Basic/DiagnosticLexKinds.td
===================================================================
--- include/clang/Basic/DiagnosticLexKinds.td
+++ include/clang/Basic/DiagnosticLexKinds.td
@@ -471,6 +471,10 @@
 def err_pp_identifier_arg_not_identifier : Error<
   "cannot convert %0 token to an identifier">;
 
+def warn_pp_filename_likely_typo :
+  ExtWarn<"likely typo, expected \"FILENAME\" or <FILENAME> "
+  "but filename is '%0'">, InGroup<UnknownPragmas>;
+
 def warn_pragma_include_alias_mismatch_angle :
    ExtWarn<"angle-bracketed include <%0> cannot be aliased to double-quoted "
    "include \"%1\"">, InGroup<UnknownPragmas>;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to