From e135cf8a03b974d2a43eb9fb93ad2d9adefcdf34 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Mon, 22 Jun 2020 15:55:59 -0700 Subject: [PATCH] Add -ast-dump-decl-types flag to include types of value and type declarations in AST dumps. Includes a testcase for deduction guide transformation that makes use of this new dumping feature. --- clang/include/clang/Driver/CC1Options.td | 2 + clang/include/clang/Frontend/ASTConsumers.h | 2 +- clang/include/clang/Frontend/FrontendOptions.h | 3 + clang/lib/Frontend/ASTConsumers.cpp | 35 ++++++--- clang/lib/Frontend/CompilerInvocation.cpp | 2 + clang/lib/Frontend/FrontendActions.cpp | 3 +- clang/test/SemaTemplate/deduction-guide.cpp | 84 ++++++++++++++++++++++ clang/tools/clang-check/ClangCheck.cpp | 1 + .../tools/clang-import-test/clang-import-test.cpp | 6 +- 9 files changed, 124 insertions(+), 14 deletions(-) create mode 100644 clang/test/SemaTemplate/deduction-guide.cpp diff --git a/clang/include/clang/Driver/CC1Options.td b/clang/include/clang/Driver/CC1Options.td index 9d2bfbf..cf2235e 100644 --- a/clang/include/clang/Driver/CC1Options.td +++ b/clang/include/clang/Driver/CC1Options.td @@ -603,6 +603,8 @@ def ast_dump_all : Flag<["-"], "ast-dump-all">, def ast_dump_all_EQ : Joined<["-"], "ast-dump-all=">, HelpText<"Build ASTs and then debug dump them in the specified format, " "forcing deserialization. Supported formats include: default, json">; +def ast_dump_decl_types : Flag<["-"], "ast-dump-decl-types">, + HelpText<"Include declaration types in AST dumps">; def templight_dump : Flag<["-"], "templight-dump">, HelpText<"Dump templight information to stdout">; def ast_dump_lookups : Flag<["-"], "ast-dump-lookups">, diff --git a/clang/include/clang/Frontend/ASTConsumers.h b/clang/include/clang/Frontend/ASTConsumers.h index af8c4a5..98cfc7c 100644 --- a/clang/include/clang/Frontend/ASTConsumers.h +++ b/clang/include/clang/Frontend/ASTConsumers.h @@ -39,7 +39,7 @@ std::unique_ptr CreateASTPrinter(std::unique_ptr OS, std::unique_ptr CreateASTDumper(std::unique_ptr OS, StringRef FilterString, bool DumpDecls, bool Deserialize, bool DumpLookups, - ASTDumpOutputFormat Format); + bool DumpDeclTypes, ASTDumpOutputFormat Format); // AST Decl node lister: prints qualified names of all filterable AST Decl // nodes. diff --git a/clang/include/clang/Frontend/FrontendOptions.h b/clang/include/clang/Frontend/FrontendOptions.h index 6069b5e..b2be330 100644 --- a/clang/include/clang/Frontend/FrontendOptions.h +++ b/clang/include/clang/Frontend/FrontendOptions.h @@ -285,6 +285,9 @@ public: /// Whether we include lookup table dumps in AST dumps. unsigned ASTDumpLookups : 1; + /// Whether we include declaration type dumps in AST dumps. + unsigned ASTDumpDeclTypes : 1; + /// Whether we are performing an implicit module build. unsigned BuildingImplicitModule : 1; diff --git a/clang/lib/Frontend/ASTConsumers.cpp b/clang/lib/Frontend/ASTConsumers.cpp index 043b254..12fda45 100644 --- a/clang/lib/Frontend/ASTConsumers.cpp +++ b/clang/lib/Frontend/ASTConsumers.cpp @@ -36,10 +36,10 @@ namespace { enum Kind { DumpFull, Dump, Print, None }; ASTPrinter(std::unique_ptr Out, Kind K, ASTDumpOutputFormat Format, StringRef FilterString, - bool DumpLookups = false) + bool DumpLookups = false, bool DumpDeclTypes = false) : Out(Out ? *Out : llvm::outs()), OwnedOut(std::move(Out)), OutputKind(K), OutputFormat(Format), FilterString(FilterString), - DumpLookups(DumpLookups) {} + DumpLookups(DumpLookups), DumpDeclTypes(DumpDeclTypes) {} void HandleTranslationUnit(ASTContext &Context) override { TranslationUnitDecl *D = Context.getTranslationUnitDecl(); @@ -91,8 +91,22 @@ namespace { } else if (OutputKind == Print) { PrintingPolicy Policy(D->getASTContext().getLangOpts()); D->print(Out, Policy, /*Indentation=*/0, /*PrintInstantiation=*/true); - } else if (OutputKind != None) + } else if (OutputKind != None) { D->dump(Out, OutputKind == DumpFull, OutputFormat); + } + + if (DumpDeclTypes) { + Decl *InnerD = D; + if (auto *TD = dyn_cast(D)) + InnerD = TD->getTemplatedDecl(); + + // FIXME: Support OutputFormat in type dumping. + // FIXME: Support combining -ast-dump-decl-types with -ast-dump-lookups. + if (auto *VD = dyn_cast(InnerD)) + VD->getType().dump(Out); + if (auto *TD = dyn_cast(InnerD)) + TD->getTypeForDecl()->dump(Out); + } } raw_ostream &Out; @@ -111,6 +125,9 @@ namespace { /// results will be output with a format determined by OutputKind. This is /// incompatible with OutputKind == Print. bool DumpLookups; + + /// Whether to dump the type for each declaration dumped. + bool DumpDeclTypes; }; class ASTDeclNodeLister : public ASTConsumer, @@ -146,13 +163,13 @@ clang::CreateASTPrinter(std::unique_ptr Out, std::unique_ptr clang::CreateASTDumper(std::unique_ptr Out, StringRef FilterString, bool DumpDecls, bool Deserialize, bool DumpLookups, - ASTDumpOutputFormat Format) { + bool DumpDeclTypes, ASTDumpOutputFormat Format) { assert((DumpDecls || Deserialize || DumpLookups) && "nothing to dump"); - return std::make_unique(std::move(Out), - Deserialize ? ASTPrinter::DumpFull : - DumpDecls ? ASTPrinter::Dump : - ASTPrinter::None, Format, - FilterString, DumpLookups); + return std::make_unique( + std::move(Out), + Deserialize ? ASTPrinter::DumpFull + : DumpDecls ? ASTPrinter::Dump : ASTPrinter::None, + Format, FilterString, DumpLookups, DumpDeclTypes); } std::unique_ptr clang::CreateASTDeclNodeLister() { diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 1cd4830..28c4fda 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -1715,6 +1715,7 @@ static InputKind ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args, case OPT_ast_dump: case OPT_ast_dump_all: case OPT_ast_dump_lookups: + case OPT_ast_dump_decl_types: Opts.ProgramAction = frontend::ASTDump; break; case OPT_ast_print: Opts.ProgramAction = frontend::ASTPrint; break; @@ -1872,6 +1873,7 @@ static InputKind ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args, Opts.ASTDumpAll = Args.hasArg(OPT_ast_dump_all, OPT_ast_dump_all_EQ); Opts.ASTDumpFilter = std::string(Args.getLastArgValue(OPT_ast_dump_filter)); Opts.ASTDumpLookups = Args.hasArg(OPT_ast_dump_lookups); + Opts.ASTDumpDeclTypes = Args.hasArg(OPT_ast_dump_decl_types); Opts.UseGlobalModuleIndex = !Args.hasArg(OPT_fno_modules_global_index); Opts.GenerateGlobalModuleIndex = Opts.UseGlobalModuleIndex; Opts.ModuleMapFiles = Args.getAllArgValues(OPT_fmodule_map_file); diff --git a/clang/lib/Frontend/FrontendActions.cpp b/clang/lib/Frontend/FrontendActions.cpp index 3a86a8d..711e733 100644 --- a/clang/lib/Frontend/FrontendActions.cpp +++ b/clang/lib/Frontend/FrontendActions.cpp @@ -79,7 +79,8 @@ ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { const FrontendOptions &Opts = CI.getFrontendOpts(); return CreateASTDumper(nullptr /*Dump to stdout.*/, Opts.ASTDumpFilter, Opts.ASTDumpDecls, Opts.ASTDumpAll, - Opts.ASTDumpLookups, Opts.ASTDumpFormat); + Opts.ASTDumpLookups, Opts.ASTDumpDeclTypes, + Opts.ASTDumpFormat); } std::unique_ptr diff --git a/clang/test/SemaTemplate/deduction-guide.cpp b/clang/test/SemaTemplate/deduction-guide.cpp new file mode 100644 index 0000000..c1ce625 --- /dev/null +++ b/clang/test/SemaTemplate/deduction-guide.cpp @@ -0,0 +1,84 @@ +// RUN: %clang_cc1 -std=c++2a -verify -ast-dump -ast-dump-decl-types -ast-dump-filter "deduction guide" %s | FileCheck %s + +template struct X {}; + +template struct A { // expected-note 2{{candidate}} + template A(X, Ts (*...qs)[Ns]); // expected-note {{candidate}} +}; +int arr1[3], arr2[3]; +short arr3[4]; +// FIXME: The CTAD deduction here succeeds, but the initialization deduction spuriously fails. +A a(X<&arr1, &arr2>{}, &arr1, &arr2, &arr3); // FIXME: expected-error {{no matching constructor}} +using AT = decltype(a); +using AT = A; + +// CHECK: Dumping : +// CHECK: FunctionTemplateDecl +// CHECK: |-TemplateTypeParmDecl {{.*}} typename depth 0 index 0 T +// CHECK: |-TemplateTypeParmDecl {{.*}} typename depth 0 index 1 ... Ts +// CHECK: |-NonTypeTemplateParmDecl {{.*}} 'Ts...' depth 0 index 2 ... Ns +// CHECK: |-NonTypeTemplateParmDecl {{.*}} 'T *' depth 0 index 3 ... Ps +// CHECK: |-CXXDeductionGuideDecl +// CHECK: | |-ParmVarDecl {{.*}} 'X' +// CHECK: | `-ParmVarDecl {{.*}} 'Ts (*)[Ns]...' pack +// CHECK: `-CXXDeductionGuideDecl +// CHECK: |-TemplateArgument type 'int [3]' +// CHECK: |-TemplateArgument pack +// CHECK: | |-TemplateArgument type 'int' +// CHECK: | |-TemplateArgument type 'int' +// CHECK: | `-TemplateArgument type 'short' +// CHECK: |-TemplateArgument pack +// CHECK: | |-TemplateArgument integral 3 +// CHECK: | |-TemplateArgument integral 3 +// CHECK: | `-TemplateArgument integral 4 +// CHECK: |-TemplateArgument pack +// CHECK: | |-TemplateArgument decl +// CHECK: | | `-Var {{.*}} 'arr1' 'int [3]' +// CHECK: | `-TemplateArgument decl +// CHECK: | `-Var {{.*}} 'arr2' 'int [3]' +// CHECK: |-ParmVarDecl {{.*}} 'X<&arr1, &arr2>':'X<&arr1, &arr2>' +// CHECK: |-ParmVarDecl {{.*}} 'int (*)[3]' +// CHECK: |-ParmVarDecl {{.*}} 'int (*)[3]' +// CHECK: `-ParmVarDecl {{.*}} 'short (*)[4]' +// CHECK: FunctionProtoType {{.*}} 'auto (X, Ts (*)[Ns]...) -> A' dependent trailing_return +// CHECK: |-InjectedClassNameType {{.*}} 'A' dependent +// CHECK: |-TemplateSpecializationType {{.*}} 'X' dependent X +// CHECK: | `-TemplateArgument expr +// CHECK: | `-PackExpansionExpr {{.*}} 'T *' +// CHECK: | `-DeclRefExpr {{.*}} 'T *' NonTypeTemplateParm {{.*}} 'Ps' 'T *' +// CHECK: `-PackExpansionType {{.*}} 'Ts (*)[Ns]...' dependent +// CHECK: `-PointerType {{.*}} 'Ts (*)[Ns]' dependent contains_unexpanded_pack +// CHECK: `-ParenType {{.*}} 'Ts [Ns]' sugar dependent contains_unexpanded_pack +// CHECK: `-DependentSizedArrayType {{.*}} 'Ts [Ns]' dependent contains_unexpanded_pack +// CHECK: |-TemplateTypeParmType {{.*}} 'Ts' dependent contains_unexpanded_pack depth 0 index 1 pack +// CHECK: | `-TemplateTypeParm {{.*}} 'Ts' +// CHECK: `-DeclRefExpr {{.*}} 'Ts' NonTypeTemplateParm {{.*}} 'Ns' 'Ts...' + +template struct B { + template B(X); +}; +B b(X{}); +using BT = decltype(b); +using BT = B; + +// CHECK: Dumping : +// CHECK: FunctionTemplateDecl +// CHECK: |-TemplateTypeParmDecl {{.*}} typename depth 0 index 0 T +// CHECK: |-NonTypeTemplateParmDecl {{.*}} 'T' depth 0 index 1 V +// CHECK: |-TemplateTypeParmDecl {{.*}} typename depth 0 index 2 U +// CHECK: |-NonTypeTemplateParmDecl {{.*}} 'type-parameter-0-2':'type-parameter-0-2' depth 0 index 3 W +// CHECK: |-CXXDeductionGuideDecl {{.*}} 'auto (X) -> B' +// CHECK: | `-ParmVarDecl {{.*}} 'X' +// CHECK: `-CXXDeductionGuideDecl {{.*}} 'auto (X) -> B' +// CHECK: |-TemplateArgument type 'char' +// CHECK: |-TemplateArgument integral 120 +// CHECK: |-TemplateArgument type 'nullptr_t' +// CHECK: |-TemplateArgument nullptr +// CHECK: `-ParmVarDecl {{.*}} 'X':'X' +// CHECK: FunctionProtoType {{.*}} 'auto (X) -> B' dependent trailing_return +// CHECK: |-InjectedClassNameType {{.*}} 'B' dependent +// CHECK: `-TemplateSpecializationType {{.*}} 'X' dependent X +// CHECK: |-TemplateArgument expr +// CHECK: | `-DeclRefExpr {{.*}} 'type-parameter-0-2':'type-parameter-0-2' NonTypeTemplateParm {{.*}} 'W' 'type-parameter-0-2':'type-parameter-0-2' +// CHECK: `-TemplateArgument expr +// CHECK: `-DeclRefExpr {{.*}} 'T' NonTypeTemplateParm {{.*}} 'V' 'T' diff --git a/clang/tools/clang-check/ClangCheck.cpp b/clang/tools/clang-check/ClangCheck.cpp index 2f59e2b..6565aa2 100644 --- a/clang/tools/clang-check/ClangCheck.cpp +++ b/clang/tools/clang-check/ClangCheck.cpp @@ -141,6 +141,7 @@ public: /*DumpDecls=*/true, /*Deserialize=*/false, /*DumpLookups=*/false, + /*DumpDeclTypes=*/false, clang::ADOF_Default); if (ASTPrint) return clang::CreateASTPrinter(nullptr, ASTDumpFilter); diff --git a/clang/tools/clang-import-test/clang-import-test.cpp b/clang/tools/clang-import-test/clang-import-test.cpp index 2245c6a..0c9bd23 100644 --- a/clang/tools/clang-import-test/clang-import-test.cpp +++ b/clang/tools/clang-import-test/clang-import-test.cpp @@ -320,9 +320,9 @@ llvm::Expected Parse(const std::string &Path, auto &CG = *static_cast(ASTConsumers.back().get()); if (ShouldDumpAST) - ASTConsumers.push_back( - CreateASTDumper(nullptr /*Dump to stdout.*/, "", true, false, false, - clang::ADOF_Default)); + ASTConsumers.push_back(CreateASTDumper(nullptr /*Dump to stdout.*/, "", + true, false, false, false, + clang::ADOF_Default)); CI.getDiagnosticClient().BeginSourceFile( CI.getCompilerInstance().getLangOpts(), -- 2.7.4