[clang] [clang-format] Add `TemplateTypeParameterKeyword` fixer (PR #192223)
via cfe-commits
cfe-commits at lists.llvm.org
Sun May 3 16:44:42 PDT 2026
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff origin/main HEAD --extensions cpp,h -- clang/lib/Format/TemplateTypeParameterKeywordFixer.cpp clang/lib/Format/TemplateTypeParameterKeywordFixer.h clang/include/clang/Format/Format.h clang/lib/Format/Format.cpp clang/unittests/Format/FormatTest.cpp --diff_from_common_commit
``````````
:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index de61695da..ed25b1d9c 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -17,10 +17,10 @@
#include "IntegerLiteralSeparatorFixer.h"
#include "NamespaceEndCommentsFixer.h"
#include "NumericLiteralCaseFixer.h"
-#include "TemplateTypeParameterKeywordFixer.h"
#include "ObjCPropertyAttributeOrderFixer.h"
#include "QualifierAlignmentFixer.h"
#include "SortJavaScriptImports.h"
+#include "TemplateTypeParameterKeywordFixer.h"
#include "UnwrappedLineFormatter.h"
#include "UsingDeclarationsSorter.h"
#include "clang/Tooling/Inclusions/HeaderIncludes.h"
@@ -492,9 +492,10 @@ template <> struct ScalarEnumerationTraits<FormatStyle::JavaScriptQuoteStyle> {
};
template <>
-struct ScalarEnumerationTraits<FormatStyle::TemplateTypeParameterKeywordOption> {
- static void enumeration(IO &IO,
- FormatStyle::TemplateTypeParameterKeywordOption &Value) {
+struct ScalarEnumerationTraits<
+ FormatStyle::TemplateTypeParameterKeywordOption> {
+ static void
+ enumeration(IO &IO, FormatStyle::TemplateTypeParameterKeywordOption &Value) {
IO.enumCase(Value, "Leave", FormatStyle::TTPS_Leave);
IO.enumCase(Value, "UseTypename", FormatStyle::TTPS_UseTypename);
IO.enumCase(Value, "UseClass", FormatStyle::TTPS_UseClass);
diff --git a/clang/lib/Format/TemplateTypeParameterKeywordFixer.cpp b/clang/lib/Format/TemplateTypeParameterKeywordFixer.cpp
index 6a200dda4..7a4b2b094 100644
--- a/clang/lib/Format/TemplateTypeParameterKeywordFixer.cpp
+++ b/clang/lib/Format/TemplateTypeParameterKeywordFixer.cpp
@@ -69,8 +69,9 @@ bool introducesTypeOrTemplateTemplateParameterName(const FormatToken *Kw) {
if (!N)
return false;
if (N->isOneOf(tok::comma, tok::greater, tok::equal, tok::colon,
- tok::kw_requires))
+ tok::kw_requires)) {
return true;
+ }
if (!N->Tok.getIdentifierInfo())
return false;
const FormatToken *AfterName = N->getNextNonComment();
@@ -95,11 +96,12 @@ bool allowsTypenameTemplateTemplateIntroducer(const FormatStyle &Style) {
case FormatStyle::LS_Cpp14:
return false;
default:
- return true;
+ return true;
}
}
-llvm::StringRef replacementKeyword(FormatStyle::TemplateTypeParameterKeywordOption O) {
+llvm::StringRef
+replacementKeyword(FormatStyle::TemplateTypeParameterKeywordOption O) {
switch (O) {
case FormatStyle::TTPS_UseTypename:
return "typename";
@@ -132,8 +134,10 @@ void processLine(AnnotatedLine *Line, const SourceManager &SM,
continue;
if (!introducesTypeOrTemplateTemplateParameterName(Tok))
continue;
- if (isTemplateTemplateParameterIntroducer(Prev) && !allowsTypenameTemplateTemplateIntroducer(Style))
+ if (isTemplateTemplateParameterIntroducer(Prev) &&
+ !allowsTypenameTemplateTemplateIntroducer(Style)) {
continue;
+ }
llvm::StringRef NewText = replacementKeyword(Opt);
if (NewText.empty() || NewText == Tok->TokenText)
@@ -142,8 +146,9 @@ void processLine(AnnotatedLine *Line, const SourceManager &SM,
SourceLocation Loc = Tok->Tok.getLocation();
unsigned Length = Tok->TokenText.size();
if (!AffectedRangeMgr.affectsCharSourceRange(
- CharSourceRange::getCharRange(Loc, Loc.getLocWithOffset(Length))))
+ CharSourceRange::getCharRange(Loc, Loc.getLocWithOffset(Length)))) {
continue;
+ }
cantFail(Fixes->add(tooling::Replacement(SM, Loc, Length, NewText.str())));
}
@@ -169,9 +174,10 @@ TemplateTypeParameterKeywordFixer::analyze(
tooling::Replacements Fixes;
const SourceManager &SM = Env.getSourceManager();
- for (AnnotatedLine *Line : AnnotatedLines)
+ for (AnnotatedLine *Line : AnnotatedLines) {
processLine(Line, SM, AffectedRangeMgr, Style,
Style.TemplateTypeParameterKeyword, &Fixes);
+ }
return {Fixes, 0};
}
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 60410d71c..f889b1abb 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -21999,7 +21999,8 @@ TEST_F(FormatTest, TemplateTypeParameterKeyword) {
S);
}
- // Verify that before C++14, the fixer doesn't produce template <template <class> typename C>.
+ // Verify that before C++14, the fixer doesn't produce template <template
+ // <class> typename C>.
{
FormatStyle S = getLLVMStyle();
S.Standard = FormatStyle::LS_Cpp14;
``````````
</details>
https://github.com/llvm/llvm-project/pull/192223
More information about the cfe-commits
mailing list