]> granicus.if.org Git - clang/commitdiff
Improve AST dumping:
authorRichard Smith <richard-llvm@metafoo.co.uk>
Tue, 12 Jan 2016 21:59:26 +0000 (21:59 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Tue, 12 Jan 2016 21:59:26 +0000 (21:59 +0000)
 1) When dumping a declaration that declares a name for a type, also dump the named type.
 2) Add a #pragma clang __debug dump X, that dumps the lookup results for X in
    the current context.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@257529 91177308-0d34-0410-b5e6-96231b3b80d8

12 files changed:
include/clang/Basic/DiagnosticLexKinds.td
include/clang/Basic/TokenKinds.def
include/clang/Parse/Parser.h
include/clang/Sema/Lookup.h
include/clang/Sema/Sema.h
lib/AST/ASTDumper.cpp
lib/Lex/Pragma.cpp
lib/Parse/ParsePragma.cpp
lib/Parse/ParseStmt.cpp
lib/Parse/Parser.cpp
lib/Sema/SemaLookup.cpp
test/Misc/ast-dump-lookups.cpp

index ed6ff20f5c427255f498282589f3d46404ce868c..2fc9664f49e55774607ddc8b776346fd5cd78cfb 100644 (file)
@@ -490,6 +490,8 @@ def warn_pragma_diagnostic_unknown_warning :
 // - #pragma __debug
 def warn_pragma_debug_unexpected_command : Warning<
   "unexpected debug command '%0'">, InGroup<IgnoredPragmas>;
+def warn_pragma_debug_missing_argument : Warning<
+  "missing argument to debug command '%0'">, InGroup<IgnoredPragmas>;
 
 def err_defined_macro_name : Error<"'defined' cannot be used as a macro name">;
 def err_paste_at_start : Error<
index 65a77accf92a2325fe1b015f95347e6fde672f92..026945141d24efb93c4def552612df65693f9c49 100644 (file)
@@ -699,6 +699,11 @@ ANNOTATION(pragma_parser_crash)
 // handles them.
 ANNOTATION(pragma_captured)
 
+// Annotation for #pragma clang __debug dump...
+// The lexer produces these so that the parser and semantic analysis can
+// look up and dump the operand.
+ANNOTATION(pragma_dump)
+
 // Annotation for #pragma ms_struct...
 // The lexer produces these so that they only take effect when the parser
 // handles them.
index 82b779879c206da93d0150efc012fa43b054ee2f..8017f19c6184ae287f5c1582c85ee004776b5284 100644 (file)
@@ -501,6 +501,10 @@ private:
   /// #pragma align...
   void HandlePragmaAlign();
 
+  /// \brief Handle the annotation token produced for
+  /// #pragma clang __debug dump...
+  void HandlePragmaDump();
+
   /// \brief Handle the annotation token produced for
   /// #pragma weak id...
   void HandlePragmaWeak();
index 87c40f0cf206200495724ce261f4d17feb330cb0..7efb19f57419884287d883b6cb3ba748faebc09d 100644 (file)
@@ -515,6 +515,7 @@ public:
     configure();
   }
 
+  void dump();
   void print(raw_ostream &);
 
   /// Suppress the diagnostics that would normally fire because of this
index c82684ee4583a6dfc5cb0c7d926a39ba947ffb74..0d49298bfc409cce1861204ac72a720fa5e79d06 100644 (file)
@@ -7629,6 +7629,9 @@ public:
   void ActOnPragmaMSInitSeg(SourceLocation PragmaLocation,
                             StringLiteral *SegmentName);
 
+  /// \brief Called on #pragma clang __debug dump II
+  void ActOnPragmaDump(Scope *S, SourceLocation Loc, IdentifierInfo *II);
+
   /// ActOnPragmaDetectMismatch - Call on well-formed \#pragma detect_mismatch
   void ActOnPragmaDetectMismatch(StringRef Name, StringRef Value);
 
index e7fee0316b69d537aeed9e0dae05055e633a0c9d..4622a75ac2c64fcf20b76c473c81be0aa40510b1 100644 (file)
@@ -1055,6 +1055,7 @@ void ASTDumper::VisitTypedefDecl(const TypedefDecl *D) {
   dumpType(D->getUnderlyingType());
   if (D->isModulePrivate())
     OS << " __module_private__";
+  dumpTypeAsChild(D->getUnderlyingType());
 }
 
 void ASTDumper::VisitEnumDecl(const EnumDecl *D) {
@@ -1226,6 +1227,7 @@ void ASTDumper::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
 void ASTDumper::VisitTypeAliasDecl(const TypeAliasDecl *D) {
   dumpName(D);
   dumpType(D->getUnderlyingType());
+  dumpTypeAsChild(D->getUnderlyingType());
 }
 
 void ASTDumper::VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D) {
@@ -1419,6 +1421,8 @@ void ASTDumper::VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D)
 void ASTDumper::VisitUsingShadowDecl(const UsingShadowDecl *D) {
   OS << ' ';
   dumpBareDeclRef(D->getTargetDecl());
+  if (auto *TD = dyn_cast<TypeDecl>(D->getUnderlyingDecl()))
+    dumpTypeAsChild(TD->getTypeForDecl());
 }
 
 void ASTDumper::VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
index 3134790ccb90c0321317277bb88e4f48f0b794ec..afb41a24077682613c0eae8be96db0474bb3b5f0 100644 (file)
@@ -876,6 +876,22 @@ struct PragmaDebugHandler : public PragmaHandler {
       Crasher.setKind(tok::annot_pragma_parser_crash);
       Crasher.setAnnotationRange(SourceRange(Tok.getLocation()));
       PP.EnterToken(Crasher);
+    } else if (II->isStr("dump")) {
+      Token Identifier;
+      PP.LexUnexpandedToken(Identifier);
+      if (auto *DumpII = Identifier.getIdentifierInfo()) {
+        Token DumpAnnot;
+        DumpAnnot.startToken();
+        DumpAnnot.setKind(tok::annot_pragma_dump);
+        DumpAnnot.setAnnotationRange(
+            SourceRange(Tok.getLocation(), Identifier.getLocation()));
+        DumpAnnot.setAnnotationValue(DumpII);
+        PP.DiscardUntilEndOfDirective();
+        PP.EnterToken(DumpAnnot);
+      } else {
+        PP.Diag(Identifier, diag::warn_pragma_debug_missing_argument)
+            << II->getName();
+      }
     } else if (II->isStr("llvm_fatal_error")) {
       llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
     } else if (II->isStr("llvm_unreachable")) {
@@ -887,7 +903,8 @@ struct PragmaDebugHandler : public PragmaHandler {
       if (MacroII)
         PP.dumpMacroInfo(MacroII);
       else
-        PP.Diag(MacroName, diag::warn_pragma_diagnostic_invalid);
+        PP.Diag(MacroName, diag::warn_pragma_debug_missing_argument)
+            << II->getName();
     } else if (II->isStr("overflow_stack")) {
       DebugOverflowStack();
     } else if (II->isStr("handle_crash")) {
index 4430eb8d03da4b760b7242e5d64fc41410303550..bc70942851e29cfd6417dc61f8000a06a101d499 100644 (file)
@@ -377,6 +377,14 @@ void Parser::HandlePragmaAlign() {
   Actions.ActOnPragmaOptionsAlign(Kind, PragmaLoc);
 }
 
+void Parser::HandlePragmaDump() {
+  assert(Tok.is(tok::annot_pragma_dump));
+  IdentifierInfo *II =
+      reinterpret_cast<IdentifierInfo *>(Tok.getAnnotationValue());
+  Actions.ActOnPragmaDump(getCurScope(), Tok.getLocation(), II);
+  ConsumeToken();
+}
+
 void Parser::HandlePragmaWeak() {
   assert(Tok.is(tok::annot_pragma_weak));
   SourceLocation PragmaLoc = ConsumeToken();
index 717bcff0c16838a3f008ee35a7334f4f0de16985..1dafae0c94c9b9cb65642b9ee8f92be1edcb2e49 100644 (file)
@@ -366,6 +366,10 @@ Retry:
   case tok::annot_pragma_loop_hint:
     ProhibitAttributes(Attrs);
     return ParsePragmaLoopHint(Stmts, OnlyStatement, TrailingElseLoc, Attrs);
+
+  case tok::annot_pragma_dump:
+    HandlePragmaDump();
+    return StmtEmpty();
   }
 
   // If we reached this code, the statement must end in a semicolon.
@@ -893,6 +897,9 @@ void Parser::ParseCompoundStatementLeadingPragmas() {
     case tok::annot_pragma_ms_vtordisp:
       HandlePragmaMSVtorDisp();
       break;
+    case tok::annot_pragma_dump:
+      HandlePragmaDump();
+      break;
     default:
       checkForPragmas = false;
       break;
index b3eeb9d58ff4d7b2aae3f5239e8670159f09a16c..ccefb3dd3f5db37f86e22e68262d48a0541a86f1 100644 (file)
@@ -668,6 +668,9 @@ Parser::ParseExternalDeclaration(ParsedAttributesWithRange &attrs,
   case tok::annot_pragma_ms_pragma:
     HandlePragmaMSPragma();
     return DeclGroupPtrTy();
+  case tok::annot_pragma_dump:
+    HandlePragmaDump();
+    return DeclGroupPtrTy();
   case tok::semi:
     // Either a C++11 empty-declaration or attribute-declaration.
     SingleDecl = Actions.ActOnEmptyDeclaration(getCurScope(),
index 1d7b15c35d12a4da50ae9baf471f178e2ea99a6f..45dc2e33da93e54be8ae4c5d300ca2e2135f3f7b 100644 (file)
@@ -650,6 +650,13 @@ void LookupResult::print(raw_ostream &Out) {
   }
 }
 
+LLVM_DUMP_METHOD void LookupResult::dump() {
+  llvm::errs() << "lookup results for " << getLookupName().getAsString()
+               << ":\n";
+  for (NamedDecl *D : *this)
+    D->dump();
+}
+
 /// \brief Lookup a builtin function, when name lookup would otherwise
 /// fail.
 static bool LookupBuiltin(Sema &S, LookupResult &R) {
@@ -4991,3 +4998,12 @@ const Sema::TypoExprState &Sema::getTypoExprState(TypoExpr *TE) const {
 void Sema::clearDelayedTypo(TypoExpr *TE) {
   DelayedTypos.erase(TE);
 }
+
+void Sema::ActOnPragmaDump(Scope *S, SourceLocation IILoc, IdentifierInfo *II) {
+  DeclarationNameInfo Name(II, IILoc);
+  LookupResult R(*this, Name, LookupAnyName, Sema::NotForRedeclaration);
+  R.suppressDiagnostics();
+  R.setHideTags(false);
+  LookupName(R, S);
+  R.dump();
+}
index 5c6da48b3afbb6e3fd0964c0be35791fb710865b..2d235010cb735e32970c6ed0f6045b5e135b7bce 100644 (file)
@@ -1,16 +1,31 @@
 // RUN: %clang_cc1 -std=c++11 -ast-dump -ast-dump-filter Test %s | FileCheck -check-prefix DECLS %s
 // RUN: %clang_cc1 -std=c++11 -ast-dump-lookups -ast-dump-filter Test %s | FileCheck -check-prefix LOOKUPS %s
 // RUN: %clang_cc1 -std=c++11 -ast-dump -ast-dump-lookups -ast-dump-filter Test %s | FileCheck -check-prefix DECLS-LOOKUPS %s
+// RUN: %clang_cc1 -std=c++11 -DPRAGMA -fsyntax-only %s 2>&1 | FileCheck -check-prefix PRAGMA %s
 
 namespace Test {
+  typedef int T;
   extern int a;
   int a = 0;
 }
 
+#ifdef PRAGMA
+#pragma clang __debug dump Test
+// PRAGMA: lookup results for Test:
+// PRAGMA-NEXT: NamespaceDecl {{.*}} Test
+// PRAGMA-NEXT: |-TypedefDecl {{.*}} T 'int'
+// PRAGMA-NEXT: | `-BuiltinType {{.*}} 'int'
+// PRAGMA-NEXT: |-VarDecl [[EXTERN_A:0x[^ ]*]] {{.*}} a 'int' extern
+// PRAGMA-NEXT: `-VarDecl {{.*}} prev [[EXTERN_A]] {{.*}} a 'int' cinit
+// PRAGMA-NEXT:   `-IntegerLiteral {{.*}} 'int' 0
+#endif
+
 namespace Test { }
 
 // DECLS: Dumping Test:
 // DECLS-NEXT: NamespaceDecl {{.*}} Test
+// DECLS-NEXT: |-TypedefDecl {{.*}} T 'int'
+// DECLS-NEXT: | `-BuiltinType {{.*}} 'int'
 // DECLS-NEXT: |-VarDecl [[EXTERN_A:0x[^ ]*]] {{.*}} a 'int' extern
 // DECLS-NEXT: `-VarDecl {{.*}} prev [[EXTERN_A]] {{.*}} a 'int' cinit
 // DECLS-NEXT:   `-IntegerLiteral {{.*}} 'int' 0
@@ -20,15 +35,15 @@ namespace Test { }
 
 // LOOKUPS: Dumping Test:
 // LOOKUPS-NEXT: StoredDeclsMap Namespace {{.*}} 'Test'
-// LOOKUPS-NEXT: `-DeclarationName 'a'
-// LOOKUPS-NEXT:   `-Var {{.*}} 'a' 'int'
+// LOOKUPS:      DeclarationName 'a'
+// LOOKUPS-NEXT: `-Var {{.*}} 'a' 'int'
 //
 // LOOKUPS: Dumping Test:
 // LOOKUPS-NEXT: Lookup map is in primary DeclContext
 
 // DECLS-LOOKUPS: Dumping Test:
 // DECLS-LOOKUPS-NEXT: StoredDeclsMap Namespace {{.*}} 'Test'
-// DECLS-LOOKUPS-NEXT: `-DeclarationName 'a'
+// DECLS-LOOKUPS:       -DeclarationName 'a'
 // DECLS-LOOKUPS-NEXT:   `-Var [[A:[^ ]*]] 'a' 'int'
 // DECLS-LOOKUPS-NEXT:     |-VarDecl [[EXTERN_A:0x[^ ]*]] {{.*}} a 'int' extern
 // DECLS-LOOKUPS-NEXT:     `-VarDecl [[A]] prev [[EXTERN_A]] {{.*}} a 'int' cinit