[clang] e135cf8 - Add -ast-dump-decl-types flag to include types of value and type
Richard Smith via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 22 16:48:02 PDT 2020
Author: Richard Smith
Date: 2020-06-22T16:47:51-07:00
New Revision: e135cf8a03b974d2a43eb9fb93ad2d9adefcdf34
URL: https://github.com/llvm/llvm-project/commit/e135cf8a03b974d2a43eb9fb93ad2d9adefcdf34
DIFF: https://github.com/llvm/llvm-project/commit/e135cf8a03b974d2a43eb9fb93ad2d9adefcdf34.diff
LOG: 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.
Added:
clang/test/SemaTemplate/deduction-guide.cpp
Modified:
clang/include/clang/Driver/CC1Options.td
clang/include/clang/Frontend/ASTConsumers.h
clang/include/clang/Frontend/FrontendOptions.h
clang/lib/Frontend/ASTConsumers.cpp
clang/lib/Frontend/CompilerInvocation.cpp
clang/lib/Frontend/FrontendActions.cpp
clang/tools/clang-check/ClangCheck.cpp
clang/tools/clang-import-test/clang-import-test.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Driver/CC1Options.td b/clang/include/clang/Driver/CC1Options.td
index 9d2bfbf949ef..cf2235ed9274 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 af8c4a517dcd..98cfc7cadc0d 100644
--- a/clang/include/clang/Frontend/ASTConsumers.h
+++ b/clang/include/clang/Frontend/ASTConsumers.h
@@ -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.
diff --git a/clang/include/clang/Frontend/FrontendOptions.h b/clang/include/clang/Frontend/FrontendOptions.h
index 6069b5eea265..b2be33032c08 100644
--- a/clang/include/clang/Frontend/FrontendOptions.h
+++ b/clang/include/clang/Frontend/FrontendOptions.h
@@ -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;
diff --git a/clang/lib/Frontend/ASTConsumers.cpp b/clang/lib/Frontend/ASTConsumers.cpp
index 043b2541b8f8..12fda45d0ef0 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<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();
@@ -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;
@@ -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<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() {
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 1cd483081dc2..28c4fda02a7d 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 3a86a8d05c49..711e7336c820 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<ASTConsumer>
diff --git a/clang/test/SemaTemplate/deduction-guide.cpp b/clang/test/SemaTemplate/deduction-guide.cpp
new file mode 100644
index 000000000000..c1ce62594fa6
--- /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<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'
diff --git a/clang/tools/clang-check/ClangCheck.cpp b/clang/tools/clang-check/ClangCheck.cpp
index 2f59e2b82d34..6565aa229030 100644
--- a/clang/tools/clang-check/ClangCheck.cpp
+++ b/clang/tools/clang-check/ClangCheck.cpp
@@ -141,6 +141,7 @@ class ClangCheckActionFactory {
/*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 2245c6aff883..0c9bd237b3ba 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<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(),
More information about the cfe-commits
mailing list