[PATCH] D112209: [clangd] IncludeCleaner: Complicated rules for enum usage

Kirill Bobyrev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 21 03:35:10 PDT 2021


kbobyrev updated this revision to Diff 381198.
kbobyrev added a comment.

Add one more test.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D112209

Files:
  clang-tools-extra/clangd/IncludeCleaner.cpp
  clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp


Index: clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
+++ clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
@@ -103,6 +103,38 @@
           "struct ^X { enum ^Language { ^CXX = 42, Python = 9000}; };",
           "int Lang = X::CXX;",
       },
+      {
+          "enum class Color;",
+          "enum class Color {};",
+      },
+      {
+          "enum class ^Color : int;",
+          "enum class Color : int {};",
+      },
+      {
+          "enum class Color : int {};",
+          "enum class Color : int;",
+      },
+      {
+          "enum class Color;",
+          "enum class Color {}; Color c;",
+      },
+      {
+          "enum class ^Color : int;",
+          "enum class Color : int {}; Color c;",
+      },
+      {
+          "enum class ^Color : char;",
+          "Color c;",
+      },
+      {
+          "enum class ^Color : char {};",
+          "Color c;",
+      },
+      {
+          "enum class ^Color;",
+          "Color c;",
+      },
       {
           // When a type is resolved via a using declaration, the
           // UsingShadowDecl is not referenced in the AST.
Index: clang-tools-extra/clangd/IncludeCleaner.cpp
===================================================================
--- clang-tools-extra/clangd/IncludeCleaner.cpp
+++ clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -35,6 +35,16 @@
   }
 
   bool VisitTagType(TagType *TT) {
+    // For enumerations we will require only the definition if it's present and
+    // the underlying type is not specified.
+    if (TT->isEnumeralType()) {
+      const auto *Enum = llvm::dyn_cast<EnumDecl>(TT->getDecl());
+      assert(Enum);
+      if (!Enum->getIntegerTypeSourceInfo() && TT->getDecl()->getDefinition()) {
+        Result.insert(TT->getDecl()->getDefinition()->getLocation());
+        return true;
+      }
+    }
     add(TT->getDecl());
     return true;
   }
@@ -80,6 +90,16 @@
     return true;
   }
 
+  // Require redeclarations only for definitions and only when the underlying
+  // type is specified.
+  bool VisitEnumDecl(EnumDecl *D) {
+    if (D != D->getDefinition() || !D->getIntegerTypeSourceInfo())
+      return false;
+    for (const auto *Redecl : D->redecls())
+      Result.insert(Redecl->getLocation());
+    return false;
+  }
+
 private:
   using Base = RecursiveASTVisitor<ReferencedLocationCrawler>;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112209.381198.patch
Type: text/x-patch
Size: 2491 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211021/6b88c235/attachment.bin>


More information about the cfe-commits mailing list