[clang] Extend -Wnonportable-include-path with a warning about trailing whitespace or dots (PR #96960)

via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 27 13:15:23 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Tobias Mayer (tobias-mayer)

<details>
<summary>Changes</summary>

Resolves #<!-- -->96064

Extends `-Wnonportable-include-path` with a warning about trailing whitespace or dots in include paths. 

![Screenshot from 2024-06-27 22-10-26](https://github.com/llvm/llvm-project/assets/56530704/9dbb7ddb-3f0e-4f8d-9eaf-dbb773baca27)


---
Full diff: https://github.com/llvm/llvm-project/pull/96960.diff


4 Files Affected:

- (modified) clang/include/clang/Basic/DiagnosticGroups.td (+3) 
- (modified) clang/include/clang/Basic/DiagnosticLexKinds.td (+5-1) 
- (modified) clang/lib/Lex/PPDirectives.cpp (+17) 
- (added) clang/test/Preprocessor/nonportable-include-trailing-whitespace.c (+11) 


``````````diff
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td
index 9b37d4bd3205b..364a2387d3d50 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -1535,3 +1535,6 @@ def BitIntExtension : DiagGroup<"bit-int-extension">;
 
 // Warnings about misuse of ExtractAPI options.
 def ExtractAPIMisuse : DiagGroup<"extractapi-misuse">;
+
+// Warnings about non-portable include paths
+def NonportablePath : DiagGroup<"nonportable-include-path">;
diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td b/clang/include/clang/Basic/DiagnosticLexKinds.td
index 12d7b8c0205ee..cf6e448d8fc36 100644
--- a/clang/include/clang/Basic/DiagnosticLexKinds.td
+++ b/clang/include/clang/Basic/DiagnosticLexKinds.td
@@ -361,7 +361,11 @@ class NonportablePath  : Warning<
   "non-portable path to file '%0'; specified path differs in case from file"
   " name on disk">;
 def pp_nonportable_path : NonportablePath,
-  InGroup<DiagGroup<"nonportable-include-path">>;
+  InGroup<NonportablePath>;
+def pp_nonportable_path_trailing_whitespace : Warning<
+  "non-portable path to file '%0'; specified path contains trailing"
+  "whitespace or dots">,
+  InGroup<NonportablePath>;
 def pp_nonportable_system_path : NonportablePath, DefaultIgnore,
   InGroup<DiagGroup<"nonportable-system-include-path">>;
 
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index a53540b12dee6..919e3edc18032 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -2098,6 +2098,23 @@ OptionalFileEntryRef Preprocessor::LookupHeaderIncludeOrImport(
     const FileEntry *LookupFromFile, StringRef &LookupFilename,
     SmallVectorImpl<char> &RelativePath, SmallVectorImpl<char> &SearchPath,
     ModuleMap::KnownHeader &SuggestedModule, bool isAngled) {
+
+  // Check for trailing whitespace or dots in the include path.
+  // This must be done before looking up the file, as Windows will still
+  // find the file even if there are trailing dots or whitespace.
+  size_t TrailingPos = Filename.find_last_not_of(" .");
+  if (TrailingPos != StringRef::npos && TrailingPos < Filename.size() - 1) {
+    StringRef TrimmedFilename = Filename.rtrim(" .");
+
+    auto Hint = isAngled
+                    ? FixItHint::CreateReplacement(
+                          FilenameRange, "<" + TrimmedFilename.str() + ">")
+                    : FixItHint::CreateReplacement(
+                          FilenameRange, "\"" + TrimmedFilename.str() + "\"");
+    Diag(FilenameTok, diag::pp_nonportable_path_trailing_whitespace)
+        << Filename << TrimmedFilename << Hint;
+  }
+
   auto DiagnoseHeaderInclusion = [&](FileEntryRef FE) {
     if (LangOpts.AsmPreprocessor)
       return;
diff --git a/clang/test/Preprocessor/nonportable-include-trailing-whitespace.c b/clang/test/Preprocessor/nonportable-include-trailing-whitespace.c
new file mode 100644
index 0000000000000..6def70a59d2e1
--- /dev/null
+++ b/clang/test/Preprocessor/nonportable-include-trailing-whitespace.c
@@ -0,0 +1,11 @@
+// REQUIRES: case-insensitive-filesystem
+// RUN: %clang_cc1 -Wall %s
+
+#include "empty_file_to_include.h " // expected-warning {{non-portable path}}
+#include "empty_file_to_include.h." // expected-warning {{non-portable path}}
+#include "empty_file_to_include.h       " // expected-warning {{non-portable path}}
+#include "empty_file_to_include.h......." // expected-warning {{non-portable path}}
+#include "empty_file_to_include.h . . . " // expected-warning {{non-portable path}}
+#include "empty_file_to_include.h.. .. " // expected-warning {{non-portable path}}
+
+#include "empty_file_to_include.h" // No warning
\ No newline at end of file

``````````

</details>


https://github.com/llvm/llvm-project/pull/96960


More information about the cfe-commits mailing list