Skip to content

Commit

Permalink
Add -ast-dump-decl-types flag to include types of value and type
Browse files Browse the repository at this point in the history
declarations in AST dumps.

Includes a testcase for deduction guide transformation that makes use of
this new dumping feature.
  • Loading branch information
zygoloid committed Jun 22, 2020
1 parent c20875a commit e135cf8
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 14 deletions.
2 changes: 2 additions & 0 deletions clang/include/clang/Driver/CC1Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -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">,
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Frontend/ASTConsumers.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ std::unique_ptr<ASTConsumer> CreateASTPrinter(std::unique_ptr<raw_ostream> OS,
std::unique_ptr<ASTConsumer>
CreateASTDumper(std::unique_ptr<raw_ostream> 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.
Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Frontend/FrontendOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ class FrontendOptions {
/// 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;

Expand Down
35 changes: 26 additions & 9 deletions clang/lib/Frontend/ASTConsumers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ namespace {
enum Kind { DumpFull, Dump, Print, None };
ASTPrinter(std::unique_ptr<raw_ostream> 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();
Expand Down Expand Up @@ -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<TemplateDecl>(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<ValueDecl>(InnerD))
VD->getType().dump(Out);
if (auto *TD = dyn_cast<TypeDecl>(InnerD))
TD->getTypeForDecl()->dump(Out);
}
}

raw_ostream &Out;
Expand All @@ -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,
Expand Down Expand Up @@ -146,13 +163,13 @@ clang::CreateASTPrinter(std::unique_ptr<raw_ostream> Out,
std::unique_ptr<ASTConsumer>
clang::CreateASTDumper(std::unique_ptr<raw_ostream> 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<ASTPrinter>(std::move(Out),
Deserialize ? ASTPrinter::DumpFull :
DumpDecls ? ASTPrinter::Dump :
ASTPrinter::None, Format,
FilterString, DumpLookups);
return std::make_unique<ASTPrinter>(
std::move(Out),
Deserialize ? ASTPrinter::DumpFull
: DumpDecls ? ASTPrinter::Dump : ASTPrinter::None,
Format, FilterString, DumpLookups, DumpDeclTypes);
}

std::unique_ptr<ASTConsumer> clang::CreateASTDeclNodeLister() {
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Frontend/FrontendActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ASTConsumer>
Expand Down
84 changes: 84 additions & 0 deletions clang/test/SemaTemplate/deduction-guide.cpp
Original file line number Diff line number Diff line change
@@ -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<auto ...> struct X {};

template<typename T, typename ...Ts> struct A { // expected-note 2{{candidate}}
template<Ts ...Ns, T *...Ps> A(X<Ps...>, 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<int[3], int, int, short>;

// CHECK: Dumping <deduction guide for A>:
// 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<Ps...>'
// 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<Ps...>, Ts (*)[Ns]...) -> A<T, Ts...>' dependent trailing_return
// CHECK: |-InjectedClassNameType {{.*}} 'A<T, Ts...>' dependent
// CHECK: |-TemplateSpecializationType {{.*}} 'X<Ps...>' 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<typename T, T V> struct B {
template<typename U, U W> B(X<W, V>);
};
B b(X<nullptr, 'x'>{});
using BT = decltype(b);
using BT = B<char, 'x'>;

// CHECK: Dumping <deduction guide for B>:
// 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<W, V>) -> B<T, V>'
// CHECK: | `-ParmVarDecl {{.*}} 'X<W, V>'
// CHECK: `-CXXDeductionGuideDecl {{.*}} 'auto (X<nullptr, 'x'>) -> B<char, 'x'>'
// CHECK: |-TemplateArgument type 'char'
// CHECK: |-TemplateArgument integral 120
// CHECK: |-TemplateArgument type 'nullptr_t'
// CHECK: |-TemplateArgument nullptr
// CHECK: `-ParmVarDecl {{.*}} 'X<nullptr, 'x'>':'X<nullptr, 'x'>'
// CHECK: FunctionProtoType {{.*}} 'auto (X<W, V>) -> B<T, V>' dependent trailing_return
// CHECK: |-InjectedClassNameType {{.*}} 'B<T, V>' dependent
// CHECK: `-TemplateSpecializationType {{.*}} 'X<W, V>' 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'
1 change: 1 addition & 0 deletions clang/tools/clang-check/ClangCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class ClangCheckActionFactory {
/*DumpDecls=*/true,
/*Deserialize=*/false,
/*DumpLookups=*/false,
/*DumpDeclTypes=*/false,
clang::ADOF_Default);
if (ASTPrint)
return clang::CreateASTPrinter(nullptr, ASTDumpFilter);
Expand Down
6 changes: 3 additions & 3 deletions clang/tools/clang-import-test/clang-import-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,9 @@ llvm::Expected<CIAndOrigins> Parse(const std::string &Path,
auto &CG = *static_cast<CodeGenerator *>(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(),
Expand Down

0 comments on commit e135cf8

Please sign in to comment.