https://github.com/nisarga3 created https://github.com/llvm/llvm-project/pull/86474
None >From 6d3343fe3894d1706baa6316d76a5277af596f91 Mon Sep 17 00:00:00 2001 From: Nisarga V <vnisa...@pe28vega.us.cray.com> Date: Tue, 19 Mar 2024 06:35:04 -0500 Subject: [PATCH 1/2] Added 'fdump-autotype inference' option for Issue #19. - Further refactoring in progress --- clang/include/clang/Basic/LangOptions.def | 1 + clang/include/clang/Driver/Options.td | 6 ++++++ clang/include/clang/Frontend/FrontendOptions.h | 6 ++++++ clang/lib/Frontend/CompilerInvocation.cpp | 7 +++++++ clang/tools/driver/cc1_main.cpp | 6 ++++++ 5 files changed, 26 insertions(+) diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def index 472fd9f093a718..6cb262c845af8b 100644 --- a/clang/include/clang/Basic/LangOptions.def +++ b/clang/include/clang/Basic/LangOptions.def @@ -100,6 +100,7 @@ LANGOPT(CPlusPlus20 , 1, 0, "C++20") LANGOPT(CPlusPlus23 , 1, 0, "C++23") LANGOPT(CPlusPlus26 , 1, 0, "C++26") LANGOPT(ObjC , 1, 0, "Objective-C") +LANGOPT(DumpAutoTypeInference , 1, 0, "Dump auto type inference information") BENIGN_LANGOPT(ObjCDefaultSynthProperties , 1, 0, "Objective-C auto-synthesized properties") BENIGN_LANGOPT(EncodeExtendedBlockSig , 1, 0, diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index aca8c9b0d5487a..542065be3c8811 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -7474,6 +7474,12 @@ def stats_file : Joined<["-"], "stats-file=">, def stats_file_append : Flag<["-"], "stats-file-append">, HelpText<"If stats should be appended to stats-file instead of overwriting it">, MarshallingInfoFlag<FrontendOpts<"AppendStats">>; + +def fdump_auto_type_inference : Flag<["-"], "fdump-auto-type-inference">, Group<f_Group>, + HelpText<"Enable dumping of auto type inference information">; +def fno_dump_auto_type_inference : Flag<["-"], "fno-dump-auto-type-inference">,Group<f_Group>, + HelpText<"Disable dumping of auto type inference information">; + def fdump_record_layouts_simple : Flag<["-"], "fdump-record-layouts-simple">, HelpText<"Dump record layout information in a simple form used for testing">, MarshallingInfoFlag<LangOpts<"DumpRecordLayoutsSimple">>; diff --git a/clang/include/clang/Frontend/FrontendOptions.h b/clang/include/clang/Frontend/FrontendOptions.h index 8085dbcbf671a6..fac6ff18ecae69 100644 --- a/clang/include/clang/Frontend/FrontendOptions.h +++ b/clang/include/clang/Frontend/FrontendOptions.h @@ -134,6 +134,9 @@ enum ActionKind { /// Run one or more source code analyses. RunAnalysis, + /// for dumping auto type inference + DumpAutoTypeInference, + /// Dump template instantiations TemplightDump, @@ -274,6 +277,9 @@ class FrontendInputFile { /// FrontendOptions - Options for controlling the behavior of the frontend. class FrontendOptions { public: +bool DumpAutoTypeInference=false; +bool shouldDumpAutoTypeInference() const { return DumpAutoTypeInference; } +void setDumpAutoTypeInference(bool Value) { DumpAutoTypeInference = Value; } /// Disable memory freeing on exit. LLVM_PREFERRED_TYPE(bool) unsigned DisableFree : 1; diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 451bdb9386f587..17d100f8ba2aa7 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -2576,6 +2576,7 @@ static const auto &getFrontendActionTable() { {frontend::RunPreprocessorOnly, OPT_Eonly}, {frontend::PrintDependencyDirectivesSourceMinimizerOutput, OPT_print_dependency_directives_minimized_source}, + {frontend::DumpAutoTypeInference, OPT_fdump_auto_type_inference}, }; return Table; @@ -2842,6 +2843,11 @@ static bool ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args, for (const auto *AA : Args.filtered(OPT_plugin_arg)) Opts.PluginArgs[AA->getValue(0)].emplace_back(AA->getValue(1)); + // Add custom flag handling for -fdump-auto-type-inference + if (Args.hasArg(OPT_fdump_auto_type_inference)) { + Opts.setDumpAutoTypeInference(true); + } + for (const std::string &Arg : Args.getAllArgValues(OPT_ftest_module_file_extension_EQ)) { std::string BlockName; @@ -4295,6 +4301,7 @@ static bool isStrictlyPreprocessorAction(frontend::ActionKind Action) { case frontend::RunAnalysis: case frontend::TemplightDump: case frontend::MigrateSource: + case frontend::DumpAutoTypeInference: return false; case frontend::DumpCompilerOptions: diff --git a/clang/tools/driver/cc1_main.cpp b/clang/tools/driver/cc1_main.cpp index b5c6be3c557bb3..a3e366ebcdeea1 100644 --- a/clang/tools/driver/cc1_main.cpp +++ b/clang/tools/driver/cc1_main.cpp @@ -191,6 +191,12 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) { bool Success = CompilerInvocation::CreateFromArgs(Clang->getInvocation(), Argv, Diags, Argv0); + // Check if the '-fdump-type-auto-reference' flag is present + if (Success && Clang->getFrontendOpts().DumpAutoTypeInference) { + llvm::outs() << "Dumping type auto reference...✔\n"; + // Add your logic to handle the flag here + // For example, you can traverse the AST and print information about 'auto' references + } if (!Clang->getFrontendOpts().TimeTracePath.empty()) { llvm::timeTraceProfilerInitialize( >From ce847613f9e504c6f6e9df0f5a976c7e3fcb913b Mon Sep 17 00:00:00 2001 From: Nisarga V <ms.nisar...@gmail.com> Date: Mon, 25 Mar 2024 01:59:59 -0500 Subject: [PATCH 2/2] Print the deduced type in the output --- clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 ++ clang/lib/Sema/SemaDecl.cpp | 8 ++++++++ clang/tools/driver/cc1_main.cpp | 2 -- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 9b5245695153ec..c1c8e2a58d1701 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -2496,6 +2496,8 @@ def err_auto_init_list_from_c : Error< "%select{initializer list|array}1 in C">; def err_auto_bitfield : Error< "cannot pass bit-field as __auto_type initializer in C">; +def note_deduced_auto_type : Note< + "deduced type of %0 is %1">; // C++1y decltype(auto) type def err_decltype_auto_invalid : Error< diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 1f4a041e88dfff..9afa30fab19639 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -13200,6 +13200,14 @@ bool Sema::DeduceVariableDeclarationType(VarDecl *VDecl, bool DirectInit, VDecl->setType(DeducedType); assert(VDecl->isLinkageValid()); + // Check if the variable declaration is in the compiled file + SourceManager &SM = getSourceManager(); + SourceLocation Loc = VDecl->getLocation(); + if (SM.isWrittenInMainFile(Loc)) { + // Print the deduced type for diagnostic purposes + llvm::outs() << "Deduced type for '" << VDecl->getNameAsString() << "': " << DeducedType.getAsString() << "\n"; + } + // In ARC, infer lifetime. if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(VDecl)) VDecl->setInvalidDecl(); diff --git a/clang/tools/driver/cc1_main.cpp b/clang/tools/driver/cc1_main.cpp index a3e366ebcdeea1..065c8b8689e979 100644 --- a/clang/tools/driver/cc1_main.cpp +++ b/clang/tools/driver/cc1_main.cpp @@ -194,8 +194,6 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) { // Check if the '-fdump-type-auto-reference' flag is present if (Success && Clang->getFrontendOpts().DumpAutoTypeInference) { llvm::outs() << "Dumping type auto reference...✔\n"; - // Add your logic to handle the flag here - // For example, you can traverse the AST and print information about 'auto' references } if (!Clang->getFrontendOpts().TimeTracePath.empty()) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits