[clang-tools-extra] [clang-tidy] Fix `UseConcisePreprocessorDirectives` check (PR #166000)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 1 08:45:36 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tidy
Author: mitchell (zeyi2)
<details>
<summary>Changes</summary>
Closes [#<!-- -->157527](https://github.com/llvm/llvm-project/issues/157527)
---
Full diff: https://github.com/llvm/llvm-project/pull/166000.diff
3 Files Affected:
- (modified) clang-tools-extra/clang-tidy/readability/UseConcisePreprocessorDirectivesCheck.cpp (+4-1)
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4)
- (modified) clang-tools-extra/test/clang-tidy/checkers/readability/use-concise-preprocessor-directives.cpp (+8)
``````````diff
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)
``````````
</details>
https://github.com/llvm/llvm-project/pull/166000
More information about the cfe-commits
mailing list