From 593814a10cb44c75b52fc525618c45af009bf3f9 Mon Sep 17 00:00:00 2001 From: Kirill Bobyrev Date: Mon, 25 Oct 2021 21:26:37 +0200 Subject: [PATCH] [clangd] IncludeCleaner: Complicated rules for enum usage Reviewed By: sammccall Differential Revision: https://reviews.llvm.org/D112209 --- clang-tools-extra/clangd/IncludeCleaner.cpp | 9 +++++++ .../clangd/unittests/IncludeCleanerTests.cpp | 24 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/clang-tools-extra/clangd/IncludeCleaner.cpp b/clang-tools-extra/clangd/IncludeCleaner.cpp index 35027306287ff..aea34a73d3f57 100644 --- a/clang-tools-extra/clangd/IncludeCleaner.cpp +++ b/clang-tools-extra/clangd/IncludeCleaner.cpp @@ -80,6 +80,15 @@ class ReferencedLocationCrawler return true; } + // Enums may be usefully forward-declared as *complete* types by specifying + // an underlying type. In this case, the definition should see the declaration + // so they can be checked for compatibility. + bool VisitEnumDecl(EnumDecl *D) { + if (D->isThisDeclarationADefinition() && D->getIntegerTypeSourceInfo()) + add(D); + return false; + } + private: using Base = RecursiveASTVisitor; diff --git a/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp b/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp index f206d87cce162..783ab773b283e 100644 --- a/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp +++ b/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp @@ -103,6 +103,30 @@ TEST(IncludeCleaner, ReferencedLocations) { "struct ^X { enum ^Language { ^CXX = 42, Python = 9000}; };", "int Lang = X::CXX;", }, + { + "enum class ^Color : int;", + "enum class Color : int {};", + }, + { + "enum class Color : int {};", + "enum class Color : int;", + }, + { + "enum class ^Color;", + "Color c;", + }, + { + "enum class ^Color : int;", + "Color c;", + }, + { + "enum class ^Color : char;", + "Color *c;", + }, + { + "enum class ^Color : char {};", + "Color *c;", + }, { // When a type is resolved via a using declaration, the // UsingShadowDecl is not referenced in the AST.