https://github.com/rosefromthedead updated https://github.com/llvm/llvm-project/pull/186770
>From 62a68eb464df19c17603f99d29a4e5f22ff911e8 Mon Sep 17 00:00:00 2001 From: Rose Hudson <[email protected]> Date: Wed, 25 Mar 2026 10:15:01 +0000 Subject: [PATCH] [clang][Lex] add -Wnonportable-include-path-separator Emit an warning when #include paths contain backslashes, with a fixit to convert them all to '/'. This can help users that build only on Windows to automatically make their #includes more portable. The warning is off by default due to being noisy and not always desirable. --- .../include/clang/Basic/DiagnosticLexKinds.td | 3 +++ clang/include/clang/Basic/SourceManager.h | 9 +++++++++ clang/lib/Lex/PPDirectives.cpp | 11 +++++++++++ clang/test/Lexer/backslash-include-win.c | 19 +++++++++++++++++++ 4 files changed, 42 insertions(+) create mode 100644 clang/test/Lexer/backslash-include-win.c diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td b/clang/include/clang/Basic/DiagnosticLexKinds.td index 5eceeced311f2..3525cc91d6f61 100644 --- a/clang/include/clang/Basic/DiagnosticLexKinds.td +++ b/clang/include/clang/Basic/DiagnosticLexKinds.td @@ -380,6 +380,9 @@ def pp_nonportable_path : NonportablePath, InGroup<DiagGroup<"nonportable-include-path">>; def pp_nonportable_system_path : NonportablePath, DefaultIgnore, InGroup<DiagGroup<"nonportable-system-include-path">>; +def pp_nonportable_path_separator : Warning< + "non-portable path to file '%0'; specified path contains backslashes">, + DefaultIgnore, InGroup<DiagGroup<"nonportable-include-path-separator">>; def pp_pragma_once_in_main_file : Warning<"#pragma once in main file">, InGroup<DiagGroup<"pragma-once-outside-header">>; diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h index bc9e97863556d..4217b8683da1e 100644 --- a/clang/include/clang/Basic/SourceManager.h +++ b/clang/include/clang/Basic/SourceManager.h @@ -1526,6 +1526,15 @@ class SourceManager : public RefCountedBase<SourceManager> { return Filename == "<scratch space>"; } + /// Returns whether \p Loc is located in a <module-includes> file. + bool isWrittenInModuleIncludes(SourceLocation Loc) const { + PresumedLoc Presumed = getPresumedLoc(Loc); + if (Presumed.isInvalid()) + return false; + StringRef Filename(Presumed.getFilename()); + return Filename == "<module-includes>"; + } + /// Returns whether \p Loc is located in a built-in or command line source. bool isInPredefinedFile(SourceLocation Loc) const { PresumedLoc Presumed = getPresumedLoc(Loc); diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp index 4a854c213926b..ebf7f6f6da606 100644 --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -2728,6 +2728,17 @@ Preprocessor::ImportAction Preprocessor::HandleHeaderIncludeOrImport( Diag(FilenameTok, DiagId) << Path << FixItHint::CreateReplacement(FilenameRange, Path); } + + bool SuppressBackslashDiag = + FilenameLoc.isMacroID() || + SourceMgr.isWrittenInBuiltinFile(FilenameLoc) || + SourceMgr.isWrittenInModuleIncludes(FilenameLoc); + if (!SuppressBackslashDiag && OriginalFilename.contains('\\')) { + std::string SuggestedPath = OriginalFilename.str(); + std::replace(SuggestedPath.begin(), SuggestedPath.end(), '\\', '/'); + Diag(FilenameTok, diag::pp_nonportable_path_separator) + << Name << FixItHint::CreateReplacement(FilenameRange, SuggestedPath); + } } switch (Action) { diff --git a/clang/test/Lexer/backslash-include-win.c b/clang/test/Lexer/backslash-include-win.c new file mode 100644 index 0000000000000..31e282c48e69c --- /dev/null +++ b/clang/test/Lexer/backslash-include-win.c @@ -0,0 +1,19 @@ +// REQUIRES: system-windows +// RUN: mkdir -p %t/backslash +// RUN: cp %S/Inputs/case-insensitive-include.h %t/backslash/case-insensitive-include.h +// RUN: %clang_cc1 -fsyntax-only -Wnonportable-include-path-separator -I%t %s 2>&1 | FileCheck %s + +#include "backslash\case-insensitive-include.h" +// CHECK: non-portable path to file +// CHECK: specified path contains backslashes +// CHECK: "backslash/case-insensitive-include.h" + +// Despite fixing the same span, nonportable-include-path is still a separate diagnostic +// that can fire at the same time. +#include "backslash\CASE-insensitive-include.h" +// CHECK: non-portable path to file +// CHECK: specified path differs in case from file name on disk +// CHECK: "backslash\case-insensitive-include.h" +// CHECK: non-portable path to file +// CHECK: specified path contains backslashes +// CHECK: "backslash/CASE-insensitive-include.h" _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
