Skip to content

[Clang] Repair the function "rParenEndsCast" to make incorrect judgments in template variable cases #120904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/TokenKinds.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Debug.h"

#define DEBUG_TYPE "format-token-annotator"
Expand All @@ -38,6 +40,10 @@ static bool mustBreakAfterAttributes(const FormatToken &Tok,

namespace {

// TODO: Add new Type modifiers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// TODO: Add new Type modifiers
// TODO: Add new Type modifiers.

llvm::SmallVector<llvm::StringRef> castIdentifiers{"__type_identity_t",
"remove_reference_t"};
Comment on lines +44 to +45
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about std::vector, std::whatever, foo::bar, etc?


/// Returns \c true if the line starts with a token that can start a statement
/// with an initializer.
static bool startsWithInitStatement(const AnnotatedLine &Line) {
Expand Down Expand Up @@ -2474,6 +2480,11 @@ class AnnotatingParser {
Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
Current.setType(TT_StringInConcatenation);
}
} else if (Style.isCpp() && Current.is(tok::kw_using)) {
if (Current.Next && Current.Next->Next && Current.Next->Next->Next) {
if (Current.Next->Next->Next->isTypeName(LangOpts))
Comment on lines +2483 to +2485
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this is too simplistic to be very useful. See the general syntax here. Also, this doesn't work at all if the using statement is in a header file.

castIdentifiers.push_back(Current.Next->TokenText);
}
} else if (Current.is(tok::l_paren)) {
if (lParenStartsCppCast(Current))
Current.setType(TT_CppCastLParen);
Expand Down Expand Up @@ -2831,8 +2842,21 @@ class AnnotatingParser {
IsQualifiedPointerOrReference(BeforeRParen, LangOpts);
bool ParensCouldEndDecl =
AfterRParen->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
if (ParensAreType && !ParensCouldEndDecl)
if (ParensAreType && !ParensCouldEndDecl) {
if (BeforeRParen->is(TT_TemplateCloser)) {
if (determineUnaryOperatorByUsage(*AfterRParen))
return true;
if (AfterRParen->isOneOf(tok::plus, tok::minus, tok::star, tok::exclaim,
tok::amp)) {
auto *Prev = BeforeRParen->MatchingParen->getPreviousNonComment();
for (auto &name : castIdentifiers)
if (Prev->TokenText == name)
return true;
return false;
Comment on lines +2852 to +2855
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (auto &name : castIdentifiers)
if (Prev->TokenText == name)
return true;
return false;
return std::find(castIdentifiers.begin(), castIdentifiers.end(), Prev->TokenText) != castIdentifiers.end();

}
}
return true;
}

// At this point, we heuristically assume that there are no casts at the
// start of the line. We assume that we have found most cases where there
Expand Down
14 changes: 14 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,20 @@ TEST_F(TokenAnnotatorTest, UnderstandsCasts) {
ASSERT_EQ(Tokens.size(), 11u) << Tokens;
EXPECT_TOKEN(Tokens[7], tok::r_paren, TT_CastRParen);

Tokens = annotate("(std::__type_identity_t<int>)-2;");
ASSERT_EQ(Tokens.size(), 12u) << Tokens;
EXPECT_TOKEN(Tokens[7], tok::r_paren, TT_CastRParen);

Tokens = annotate("template <typename> using type = int;\n"
"auto = (type<int>)+5;");
ASSERT_EQ(Tokens.size(), 21u) << Tokens;
EXPECT_TOKEN(Tokens[16], tok::r_paren, TT_CastRParen);

Tokens = annotate("template <class t> t c;"
"auto = (c<int>) + 5;");
ASSERT_EQ(Tokens.size(), 20u) << Tokens;
EXPECT_TOKEN(Tokens[15], tok::r_paren, TT_Unknown);

Tokens = annotate("return (Foo)p;");
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
EXPECT_TOKEN(Tokens[3], tok::r_paren, TT_CastRParen);
Expand Down
Loading