[PATCH] D51333: Diagnose likely typos in include statements

Christy Lee via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 27 16:59:12 PDT 2018


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>'}}
+ at 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>;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D51333.162779.patch
Type: text/x-patch
Size: 2032 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180827/e7c88ffe/attachment.bin>


More information about the cfe-commits mailing list