[clang] 8a3de13 - [clang-format] Disallow templates to be followed by literal
Emilia Dreamer via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 26 17:57:28 PST 2023
Author: Emilia Dreamer
Date: 2023-01-27T04:01:11+02:00
New Revision: 8a3de13573bdeaee13ec959fa2af0d11f21f5f00
URL: https://github.com/llvm/llvm-project/commit/8a3de13573bdeaee13ec959fa2af0d11f21f5f00
DIFF: https://github.com/llvm/llvm-project/commit/8a3de13573bdeaee13ec959fa2af0d11f21f5f00.diff
LOG: [clang-format] Disallow templates to be followed by literal
There should not be any cases where the angle brackets of template
parameters are directly followed by a literal. It is more likely that a
comparison is taking place instead.
This patch makes the TokenAnnotator prefer to annotate < and > as
operators when directly followed by a literal. A similar check already
exists for literals directly *before* potential template args.
Fixes https://github.com/llvm/llvm-project/issues/60140
Reviewed By: HazardyKnusperkeks, owenpan, MyDeveloperDay
Differential Revision: https://reviews.llvm.org/D142139
Added:
Modified:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 49c30ca78debf..1b1123fa36456 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -185,6 +185,8 @@ class AnnotatingParser {
} else {
CurrentToken->setType(TT_TemplateCloser);
}
+ if (CurrentToken->Next && CurrentToken->Next->Tok.isLiteral())
+ return false;
next();
return true;
}
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 6530e2005e4c4..1d71bc909e8c6 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -10365,6 +10365,7 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) {
// Not template parameters.
verifyFormat("return a < b && c > d;");
+ verifyFormat("a < 0 ? b : a > 0 ? c : d;");
verifyFormat("void f() {\n"
" while (a < b && c > d) {\n"
" }\n"
More information about the cfe-commits
mailing list