https://github.com/tobias-mayer created https://github.com/llvm/llvm-project/pull/96960
Resolves #96064 Extends `-Wnonportable-include-path` with a warning about trailing whitespace or dots in include paths.  >From ec07df7ff56b21170c4a0d61b119426bf4dfeff7 Mon Sep 17 00:00:00 2001 From: Tobias Mayer <tobi.mayer1...@gmail.com> Date: Thu, 27 Jun 2024 22:04:55 +0200 Subject: [PATCH] add warning about trailing whitespace or dots in include paths --- clang/include/clang/Basic/DiagnosticGroups.td | 3 +++ clang/include/clang/Basic/DiagnosticLexKinds.td | 6 +++++- clang/lib/Lex/PPDirectives.cpp | 17 +++++++++++++++++ .../nonportable-include-trailing-whitespace.c | 11 +++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 clang/test/Preprocessor/nonportable-include-trailing-whitespace.c 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 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits