[PATCH] D155273: [clang-format] Add TypeNames option to disambiguate types/objects

2023-07-18 Thread Owen Pan via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG5c106f7b947e: [clang-format] Add TypeNames option to 
disambiguate types/objects (authored by owenpan).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155273/new/

https://reviews.llvm.org/D155273

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/FormatToken.h
  clang/lib/Format/FormatTokenLexer.cpp
  clang/lib/Format/FormatTokenLexer.h
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/TokenAnnotatorTest.cpp

Index: clang/unittests/Format/TokenAnnotatorTest.cpp
===
--- clang/unittests/Format/TokenAnnotatorTest.cpp
+++ clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -272,6 +272,19 @@
   Tokens = annotate("template * = nullptr> void f();");
   ASSERT_EQ(Tokens.size(), 19u) << Tokens;
   EXPECT_TOKEN(Tokens[5], tok::ampamp, TT_BinaryOperator);
+
+  FormatStyle Style = getLLVMStyle();
+  Style.TypeNames.push_back("MYI");
+  Tokens = annotate("if (MYI *p{nullptr})", Style);
+  ASSERT_EQ(Tokens.size(), 10u) << Tokens;
+  EXPECT_TOKEN(Tokens[2], tok::identifier, TT_TypeName);
+  EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
+
+  Style.TypeNames.push_back("Class");
+  Tokens = annotate("if (Class *obj {getObj()})", Style);
+  ASSERT_EQ(Tokens.size(), 12u) << Tokens;
+  EXPECT_TOKEN(Tokens[2], tok::identifier, TT_TypeName);
+  EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
 }
 
 TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -422,6 +422,7 @@
   FormatToken *PrevPrev = Prev->getPreviousNonComment();
   FormatToken *Next = CurrentToken->Next;
   if (PrevPrev && PrevPrev->is(tok::identifier) &&
+  PrevPrev->isNot(TT_TypeName) &&
   Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
   CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
 Prev->setType(TT_BinaryOperator);
@@ -2508,6 +2509,8 @@
 const FormatToken *PrevToken = Tok.getPreviousNonComment();
 if (!PrevToken)
   return TT_UnaryOperator;
+if (PrevToken->is(TT_TypeName))
+  return TT_PointerOrReference;
 
 const FormatToken *NextToken = Tok.getNextNonComment();
 
Index: clang/lib/Format/FormatTokenLexer.h
===
--- clang/lib/Format/FormatTokenLexer.h
+++ clang/lib/Format/FormatTokenLexer.h
@@ -22,6 +22,7 @@
 #include "clang/Basic/SourceManager.h"
 #include "clang/Format/Format.h"
 #include "llvm/ADT/MapVector.h"
+#include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/StringSet.h"
 #include "llvm/Support/Regex.h"
 
@@ -126,6 +127,8 @@
 
   llvm::SmallMapVector Macros;
 
+  llvm::SmallPtrSet TypeNames;
+
   bool FormattingDisabled;
 
   llvm::Regex MacroBlockBeginRegex;
Index: clang/lib/Format/FormatTokenLexer.cpp
===
--- clang/lib/Format/FormatTokenLexer.cpp
+++ clang/lib/Format/FormatTokenLexer.cpp
@@ -71,6 +71,9 @@
 auto Identifier = &IdentTable.get(StatementAttributeLikeMacro);
 Macros.insert({Identifier, TT_StatementAttributeLikeMacro});
   }
+
+  for (const auto &TypeName : Style.TypeNames)
+TypeNames.insert(&IdentTable.get(TypeName));
 }
 
 ArrayRef FormatTokenLexer::lex() {
@@ -1222,7 +1225,8 @@
   }
 
   if (Style.isCpp()) {
-auto it = Macros.find(FormatTok->Tok.getIdentifierInfo());
+auto *Identifier = FormatTok->Tok.getIdentifierInfo();
+auto it = Macros.find(Identifier);
 if (!(Tokens.size() > 0 && Tokens.back()->Tok.getIdentifierInfo() &&
   Tokens.back()->Tok.getIdentifierInfo()->getPPKeywordID() ==
   tok::pp_define) &&
@@ -1240,6 +1244,8 @@
 FormatTok->setType(TT_MacroBlockBegin);
   else if (MacroBlockEndRegex.match(Text))
 FormatTok->setType(TT_MacroBlockEnd);
+  else if (TypeNames.contains(Identifier))
+FormatTok->setFinalizedType(TT_TypeName);
 }
   }
 
Index: clang/lib/Format/FormatToken.h
===
--- clang/lib/Format/FormatToken.h
+++ clang/lib/Format/FormatToken.h
@@ -141,6 +141,7 @@
   TYPE(TrailingReturnArrow)\
   TYPE(TrailingUnaryOperator)  \
   TYPE(TypeDeclarationParen)   \
+  TYPE(TypeName)   \
   TYPE(TypenameMacro)  

[PATCH] D155273: [clang-format] Add TypeNames option to disambiguate types/objects

2023-07-18 Thread MyDeveloperDay via Phabricator via cfe-commits
MyDeveloperDay added a comment.

Nice!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155273/new/

https://reviews.llvm.org/D155273

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155273: [clang-format] Add TypeNames option to disambiguate types/objects

2023-07-19 Thread MyDeveloperDay via Phabricator via cfe-commits
MyDeveloperDay added a comment.

I used isSimpleTypeSpecifier() in the east/west const fixer, I think this could 
probably help there to catch more places too.. (you don't need to do that here)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155273/new/

https://reviews.llvm.org/D155273

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155273: [clang-format] Add TypeNames option to disambiguate types/objects

2023-07-14 Thread Owen Pan via Phabricator via cfe-commits
owenpan created this revision.
Herald added projects: All, clang, clang-format.
Herald added a subscriber: cfe-commits.
Herald added reviewers: rymiel, HazardyKnusperkeks, MyDeveloperDay.
owenpan requested review of this revision.

If a non-keyword identifier is found in `TypeNames`, then a `*`, `&`, or `&&` 
that follows it is annotated as `TT_PointerOrReference`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155273

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/FormatToken.h
  clang/lib/Format/FormatTokenLexer.cpp
  clang/lib/Format/FormatTokenLexer.h
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/TokenAnnotatorTest.cpp

Index: clang/unittests/Format/TokenAnnotatorTest.cpp
===
--- clang/unittests/Format/TokenAnnotatorTest.cpp
+++ clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -272,6 +272,19 @@
   Tokens = annotate("template * = nullptr> void f();");
   ASSERT_EQ(Tokens.size(), 19u) << Tokens;
   EXPECT_TOKEN(Tokens[5], tok::ampamp, TT_BinaryOperator);
+
+  FormatStyle Style = getLLVMStyle();
+  Style.TypeNames.push_back("MYI");
+  Tokens = annotate("if (MYI *p{nullptr})", Style);
+  ASSERT_EQ(Tokens.size(), 10u) << Tokens;
+  EXPECT_TOKEN(Tokens[2], tok::identifier, TT_TypeName);
+  EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
+
+  Style.TypeNames.push_back("Class");
+  Tokens = annotate("if (Class *obj {getObj()})", Style);
+  ASSERT_EQ(Tokens.size(), 12u) << Tokens;
+  EXPECT_TOKEN(Tokens[2], tok::identifier, TT_TypeName);
+  EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
 }
 
 TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -422,6 +422,7 @@
   FormatToken *PrevPrev = Prev->getPreviousNonComment();
   FormatToken *Next = CurrentToken->Next;
   if (PrevPrev && PrevPrev->is(tok::identifier) &&
+  PrevPrev->isNot(TT_TypeName) &&
   Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
   CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
 Prev->setType(TT_BinaryOperator);
@@ -2508,6 +2509,8 @@
 const FormatToken *PrevToken = Tok.getPreviousNonComment();
 if (!PrevToken)
   return TT_UnaryOperator;
+if (PrevToken->is(TT_TypeName))
+  return TT_PointerOrReference;
 
 const FormatToken *NextToken = Tok.getNextNonComment();
 
Index: clang/lib/Format/FormatTokenLexer.h
===
--- clang/lib/Format/FormatTokenLexer.h
+++ clang/lib/Format/FormatTokenLexer.h
@@ -22,6 +22,7 @@
 #include "clang/Basic/SourceManager.h"
 #include "clang/Format/Format.h"
 #include "llvm/ADT/MapVector.h"
+#include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/StringSet.h"
 #include "llvm/Support/Regex.h"
 
@@ -126,6 +127,8 @@
 
   llvm::SmallMapVector Macros;
 
+  llvm::SmallPtrSet TypeNames;
+
   bool FormattingDisabled;
 
   llvm::Regex MacroBlockBeginRegex;
Index: clang/lib/Format/FormatTokenLexer.cpp
===
--- clang/lib/Format/FormatTokenLexer.cpp
+++ clang/lib/Format/FormatTokenLexer.cpp
@@ -71,6 +71,9 @@
 auto Identifier = &IdentTable.get(StatementAttributeLikeMacro);
 Macros.insert({Identifier, TT_StatementAttributeLikeMacro});
   }
+
+  for (const auto &TypeName : Style.TypeNames)
+TypeNames.insert(&IdentTable.get(TypeName));
 }
 
 ArrayRef FormatTokenLexer::lex() {
@@ -1222,7 +1225,8 @@
   }
 
   if (Style.isCpp()) {
-auto it = Macros.find(FormatTok->Tok.getIdentifierInfo());
+auto *Identifier = FormatTok->Tok.getIdentifierInfo();
+auto it = Macros.find(Identifier);
 if (!(Tokens.size() > 0 && Tokens.back()->Tok.getIdentifierInfo() &&
   Tokens.back()->Tok.getIdentifierInfo()->getPPKeywordID() ==
   tok::pp_define) &&
@@ -1240,6 +1244,8 @@
 FormatTok->setType(TT_MacroBlockBegin);
   else if (MacroBlockEndRegex.match(Text))
 FormatTok->setType(TT_MacroBlockEnd);
+  else if (TypeNames.contains(Identifier))
+FormatTok->setFinalizedType(TT_TypeName);
 }
   }
 
Index: clang/lib/Format/FormatToken.h
===
--- clang/lib/Format/FormatToken.h
+++ clang/lib/Format/FormatToken.h
@@ -141,6 +141,7 @@
   TYPE(TrailingReturnArrow)\
   TYPE(TrailingUnaryOperator)  \
   TYPE(TypeDeclarationParen)   \
+  TYPE(TypeName)   \
   TYPE

[PATCH] D155273: [clang-format] Add TypeNames option to disambiguate types/objects

2023-07-14 Thread Owen Pan via Phabricator via cfe-commits
owenpan added inline comments.



Comment at: clang/unittests/Format/TokenAnnotatorTest.cpp:284
+  Style.TypeNames.push_back("Class");
+  Tokens = annotate("if (Class *obj {getObj()})", Style);
+  ASSERT_EQ(Tokens.size(), 12u) << Tokens;

This test case comes from D137327.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155273/new/

https://reviews.llvm.org/D155273

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155273: [clang-format] Add TypeNames option to disambiguate types/objects

2023-07-14 Thread Tobias Hieta via Phabricator via cfe-commits
thieta added a comment.

Thanks for doing this!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155273/new/

https://reviews.llvm.org/D155273

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits