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 ` check to recognize literal suffixes added in C++23 and C23. +- Improved :doc:`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)