[clang] [clang][Lex] warn on backslashes in include paths (PR #186770)
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 16 07:44:38 PDT 2026
================
@@ -2719,14 +2721,43 @@ Preprocessor::ImportAction Preprocessor::HandleHeaderIncludeOrImport(
Path = (Path.substr(0, 1) + "\\\\?\\" + Path.substr(1)).str();
#endif
+ SuggestedPath = std::make_optional(std::move(Path));
+ HasCaseDifference = true;
+ }
+
+ bool HasReplaceableBackslash = false;
+ if (Name.contains('\\')) {
+ if (!SuggestedPath.has_value()) {
+ SuggestedPath = std::make_optional(OriginalFilename);
+ }
+ std::replace(SuggestedPath->begin(), SuggestedPath->end(), '\\', '/');
+ HasReplaceableBackslash = true;
+ }
+
+ SourceLocation FilenameLoc = FilenameTok.getLocation();
+ bool SuppressDiag = FilenameLoc.isMacroID() ||
+ SourceMgr.isWrittenInBuiltinFile(FilenameLoc) ||
+ SourceMgr.isWrittenInModuleIncludes(FilenameLoc) ||
+ SourceMgr.isWrittenInExtractAPIIncludes(FilenameLoc);
+ if (SuggestedPath.has_value() && !SuppressDiag) {
// For user files and known standard headers, issue a diagnostic.
// For other system headers, don't. They can be controlled separately.
auto DiagId =
(FileCharacter == SrcMgr::C_User || warnByDefaultOnWrongCase(Name))
? diag::pp_nonportable_path
: diag::pp_nonportable_system_path;
- Diag(FilenameTok, DiagId) << Path <<
- FixItHint::CreateReplacement(FilenameRange, Path);
+ auto Diagnostic =
+ Diag(FilenameTok, DiagId)
+ << SuggestedPath.value()
+ << FixItHint::CreateReplacement(FilenameRange, SuggestedPath.value());
+ if (HasCaseDifference && HasReplaceableBackslash) {
+ Diagnostic << "specified path contains backslashes and differs in case "
+ "from file name on disk";
+ } else if (HasReplaceableBackslash) {
+ Diagnostic << "specified path contains backslashes";
+ } else if (HasCaseDifference) {
+ Diagnostic << "specified path differs in case from file name on disk";
+ }
----------------
AaronBallman wrote:
Please specify this via a `%select` in the .td file instead of hard-coding strings here (we prefer diagnostic text to live in the .td file for localization purposes).
https://github.com/llvm/llvm-project/pull/186770
More information about the cfe-commits
mailing list