[PATCH] D52529: [Frontend] Simplify -print-decl-contexts with DeclNodes.inc

Richard Smith - zygoloid via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 1 14:32:28 PDT 2018


rsmith added a comment.

Do we need `-print-decl-contexts` at all? It's not exposed by the driver, not tested, not documented, has a very strange output format (the bracketing character supplies some information about the kind of declaration, but it's not clear *what* information), and it doesn't really seem to provide any value given that we also have the maintained, tested, and actively-used `-ast-dump` flag, as well as programmatic interfaces to this information via libclang.

The only reason I can see to keep it would be if there's some external tool that's parsing its output, which this patch would break.



================
Comment at: lib/Frontend/ASTConsumers.cpp:423
+    if (auto *DC = dyn_cast<DeclContext>(I)) {
+      PrintDeclContext(DC, Indentation + 1);
+      continue;
----------------
Was the change from +2 to +1 here intentional?


================
Comment at: lib/Frontend/ASTConsumers.cpp:427-431
     switch (DK) {
-    case Decl::Namespace:
-    case Decl::Enum:
-    case Decl::Record:
-    case Decl::CXXRecord:
-    case Decl::ObjCMethod:
-    case Decl::ObjCInterface:
-    case Decl::ObjCCategory:
-    case Decl::ObjCProtocol:
-    case Decl::ObjCImplementation:
-    case Decl::ObjCCategoryImpl:
-    case Decl::LinkageSpec:
-    case Decl::Block:
-    case Decl::Function:
-    case Decl::CXXMethod:
-    case Decl::CXXConstructor:
-    case Decl::CXXDestructor:
-    case Decl::CXXConversion:
-    case Decl::ClassTemplateSpecialization:
-    case Decl::ClassTemplatePartialSpecialization: {
-      DeclContext* DC = cast<DeclContext>(I);
-      PrintDeclContext(DC, Indentation+2);
-      break;
-    }
-    case Decl::IndirectField:
-      Out << "<IndirectField> " << *cast<IndirectFieldDecl>(I) << '\n';
-      break;
-    case Decl::Label:
-      Out << "<Label> " << *cast<LabelDecl>(I) << '\n';
-      break;
-    case Decl::Field:
-      Out << "<field> " << *cast<FieldDecl>(I) << '\n';
-      break;
-    case Decl::Typedef:
-    case Decl::TypeAlias:
-      Out << "<typedef> " << *cast<TypedefNameDecl>(I) << '\n';
-      break;
-    case Decl::EnumConstant:
-      Out << "<enum constant> " << *cast<EnumConstantDecl>(I) << '\n';
-      break;
-    case Decl::Var:
-      Out << "<var> " << *cast<VarDecl>(I) << '\n';
-      break;
-    case Decl::ImplicitParam:
-      Out << "<implicit parameter> " << *cast<ImplicitParamDecl>(I) << '\n';
-      break;
-    case Decl::ParmVar:
-      Out << "<parameter> " << *cast<ParmVarDecl>(I) << '\n';
-      break;
-    case Decl::ObjCProperty:
-      Out << "<objc property> " << *cast<ObjCPropertyDecl>(I) << '\n';
-      break;
-    case Decl::FunctionTemplate:
-      Out << "<function template> " << *cast<FunctionTemplateDecl>(I) << '\n';
-      break;
-    case Decl::TypeAliasTemplate:
-      Out << "<type alias template> " << *cast<TypeAliasTemplateDecl>(I)
-          << '\n';
-      break;
-    case Decl::FileScopeAsm:
-      Out << "<file-scope asm>\n";
-      break;
-    case Decl::UsingDirective:
-      Out << "<using directive>\n";
-      break;
-    case Decl::NamespaceAlias:
-      Out << "<namespace alias> " << *cast<NamespaceAliasDecl>(I) << '\n';
-      break;
-    case Decl::ClassTemplate:
-      Out << "<class template> " << *cast<ClassTemplateDecl>(I) << '\n';
-      break;
-    case Decl::OMPThreadPrivate: {
-      Out << "<omp threadprivate> " << '"' << I << "\"\n";
-      break;
-    }
-    case Decl::Friend: {
-      Out << "<friend>";
-      if (const NamedDecl *ND = cast<FriendDecl>(I)->getFriendDecl())
-        Out << ' ' << *ND;
-      Out << "\n";
-      break;
-    }
-    case Decl::Using:
-      Out << "<using> " << *cast<UsingDecl>(I) << "\n";
-      break;
-    case Decl::UsingShadow:
-      Out << "<using shadow> " << *cast<UsingShadowDecl>(I) << "\n";
-      break;
-    case Decl::UnresolvedUsingValue:
-      Out << "<unresolved using value> " << *cast<UnresolvedUsingValueDecl>(I)
-          << "\n";
-      break;
-    case Decl::Empty:
-      Out << "<empty>\n";
-      break;
-    case Decl::AccessSpec:
-      Out << "<access specifier>\n";
-      break;
-    case Decl::VarTemplate:
-      Out << "<var template> " << *cast<VarTemplateDecl>(I) << "\n";
-      break;
-    case Decl::StaticAssert:
-      Out << "<static assert>\n";
-      break;
-
-    default:
-      Out << "DeclKind: " << DK << '"' << I << "\"\n";
-      llvm_unreachable("decl unhandled");
+#define NAMED(DERIVED, BASE)                                         \
+    case Decl::DERIVED:                                              \
+      Out << "<" << #DERIVED << "> " << *cast<NamedDecl>(I) << "\n"; \
+      continue;
----------------
You don't need a switch for this either. `if (auto *ND = dyn_cast<NamedDecl>(I))` and `ND->getDeclKindName()` should work fine.


================
Comment at: lib/Frontend/ASTConsumers.cpp:498-499
-      Out << "<friend>";
-      if (const NamedDecl *ND = cast<FriendDecl>(I)->getFriendDecl())
-        Out << ' ' << *ND;
-      Out << "\n";
----------------
You've lost this special case.


Repository:
  rC Clang

https://reviews.llvm.org/D52529





More information about the cfe-commits mailing list