[clang-tools-extra] [clang-tidy] Fix `UseConcisePreprocessorDirectives` check (PR #166000)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 1 08:45:06 PDT 2025
https://github.com/zeyi2 created https://github.com/llvm/llvm-project/pull/166000
Closes [#157527](https://github.com/llvm/llvm-project/issues/157527)
>From 6a6156b7e3ccb3c9edc5e2a54baf7e2cb7b241fa Mon Sep 17 00:00:00 2001
From: mtx <mitchell.xu2 at gmail.com>
Date: Sat, 1 Nov 2025 23:14:47 +0800
Subject: [PATCH] [clang-tidy] Fix `UseConcisePreprocessorDirectives` check
---
.../readability/UseConcisePreprocessorDirectivesCheck.cpp | 5 ++++-
clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++
.../readability/use-concise-preprocessor-directives.cpp | 8 ++++++++
3 files changed, 16 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/clang-tidy/readability/UseConcisePreprocessorDirectivesCheck.cpp b/clang-tools-extra/clang-tidy/readability/UseConcisePreprocessorDirectivesCheck.cpp
index 40aaff4cb3893..ef495d3bf0f6e 100644
--- a/clang-tools-extra/clang-tidy/readability/UseConcisePreprocessorDirectivesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/UseConcisePreprocessorDirectivesCheck.cpp
@@ -91,7 +91,10 @@ class IfPreprocessorCallbacks final : public PPCallbacks {
Check.diag(
DirectiveLoc,
"preprocessor condition can be written more concisely using '#%0'")
- << FixItHint::CreateReplacement(DirectiveLoc, Replacements[Inverted])
+ << FixItHint::CreateReplacement(
+ CharSourceRange::getCharRange(DirectiveLoc,
+ ConditionRange.getBegin()),
+ (Replacements[Inverted].str() + " "))
<< FixItHint::CreateReplacement(ConditionRange, Macro)
<< Replacements[Inverted];
}
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 8f4be0d1cb259..b79112a35a8c4 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -454,6 +454,10 @@ Changes in existing checks
<clang-tidy/checks/readability/uppercase-literal-suffix>` check to recognize
literal suffixes added in C++23 and C23.
+- Improved :doc:`readability-use-concise-preprocessor-directives
+ <clang-tidy/checks/readability/use-concise-preprocessor-directives>` check to
+ generate correct fix-its for forms without a space after the directive.
+
Removed checks
^^^^^^^^^^^^^^
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/use-concise-preprocessor-directives.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/use-concise-preprocessor-directives.cpp
index 53e079bcca40f..b8a4953161d86 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/use-concise-preprocessor-directives.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/use-concise-preprocessor-directives.cpp
@@ -28,6 +28,14 @@
# elif (defined(BAR))
#endif
+// CHECK-MESSAGES: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#ifdef' [readability-use-concise-preprocessor-directives]
+// CHECK-FIXES: #ifdef FOO
+#if(defined(FOO))
+// CHECK-MESSAGES-23: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#elifdef' [readability-use-concise-preprocessor-directives]
+// CHECK-FIXES-23: #elifdef BAR
+#elif(defined(BAR))
+#endif
+
// CHECK-MESSAGES: :[[@LINE+2]]:2: warning: preprocessor condition can be written more concisely using '#ifdef' [readability-use-concise-preprocessor-directives]
// CHECK-FIXES: #ifdef FOO
#if (defined FOO)
More information about the cfe-commits
mailing list