[clang] Issue #19 and #20 - Describe auto (C++11) type inferences (PR #86474)
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 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>; + +def fdump_auto_type_inference : Flag<["-"], "fdump-auto-type-inference">, Group, +HelpText<"Enable dumping of auto type inference information">; +def fno_dump_auto_type_inference : Flag<["-"], "fno-dump-auto-type-inference">,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>; 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 Argv, const char *Argv0, void *MainAddr) { bool Success = CompilerInvocation::CreateFromArgs(Clang->getInvocation(),
[clang] Issue #19 and #20 - Describe auto (C++11) type inferences (PR #86474)
https://github.com/nisarga3 closed https://github.com/llvm/llvm-project/pull/86474 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Dump Auto Type Inference (PR #95509)
https://github.com/nisarga3 created https://github.com/llvm/llvm-project/pull/95509 This pull request introduces the functionality to dump type inferences for variables and function return types using the auto keyword in C++11. When the -fdump-auto-type-inference option is specified, the compiler will emit informational messages that describe the inferred types for auto declarations. **Problem Statement :** C++11's auto keyword allows developers to let the compiler deduce the type of a variable or the return type of a function, which simplifies code and reduces redundancy. However, this can sometimes obscure the actual types being used, making it difficult to understand the code's behaviour, especially in complex codebases or during debugging. Developers need a way to see the types inferred by the compiler to improve their understanding and confidence in the code. **Proposed Solution** The proposed solution is to implement a compiler feature that dumps the inferred types of variables and function return types when the -fdump-auto-type-inference option is used. This feature will output informational messages indicating the deduced types, providing clarity and aiding in debugging. **Compilation Command (from build directory)** `./bin/clang++ -mllvm -fdump-auto-type-inference ./Hello.cpp` Hello.cpp ``` #include using namespace std; void testAuto() { auto w = 5; auto z = 3.14; auto add = [](auto a, auto b) { return a + b; }; } int main() { testAuto(); auto x = 5;// int auto y = 3.14; // double auto z = 'c'; // char auto arr = {1, 2, 3}; // std::initializer_list auto add = [](auto a, auto b) { return a + b; }; auto divide = [](auto a, auto b) -> decltype(a / b) { return a / b; }; struct Foo { auto getVal() const { return val; } int val = 42; }; return 0; } ``` Output ``` ../hello.cpp:5:8: remark: type of 'w' deduced as 'int' 5 | auto w = 5; |^ ../hello.cpp:6:8: remark: type of 'z' deduced as 'double' 6 | auto z = 3.14; |^ ../hello.cpp:8:8: remark: type of 'add' deduced as '(lambda at ../hello.cpp:8:14)' 8 | auto add = [](auto a, auto b) { |^ ../hello.cpp:16:10: remark: type of 'x' deduced as 'int' 16 | auto x = 5;// int | ^ ../hello.cpp:17:10: remark: type of 'y' deduced as 'double' 17 | auto y = 3.14; // double | ^ ../hello.cpp:18:10: remark: type of 'z' deduced as 'char' 18 | auto z = 'c'; // char | ^ ../hello.cpp:19:10: remark: type of 'arr' deduced as 'std::initializer_list' 19 | auto arr = {1, 2, 3}; // std::initializer_list | ^ ../hello.cpp:27:10: remark: type of 'add' deduced as '(lambda at ../hello.cpp:27:16)' 27 | auto add = [](auto a, auto b) { | ^ ../hello.cpp:31:10: remark: type of 'divide' deduced as '(lambda at ../hello.cpp:31:19)' 31 | auto divide = [](auto a, auto b) -> decltype(a / b) { | ^ ../hello.cpp:36:14: remark: return type of function 'getVal' deduced as 'int' 36 | auto getVal() const { | ^ ``` **Benefits to the Community** - Improved Code Comprehension: Developers can easily see the types inferred by the compiler, which enhances their understanding of the code. - Enhanced Debugging: During debugging, knowing the exact types can help diagnose type-related issues more efficiently. >From df232a67ac0f5a294e8db4c86e10b6bdf664d673 Mon Sep 17 00:00:00 2001 From: Nisarga V Date: Fri, 14 Jun 2024 01:16:38 -0500 Subject: [PATCH] [clang] Dump Auto Type Inference: This pull request adds the -fdump-auto-type-inference option to Clang. When enabled, the compiler will emit messages describing the inferred types for auto declarations in C++11. This feature aids in understanding and debugging by providing clarity on the types deduced by the compiler. --- clang/include/clang/Driver/Options.td | 4 ++ clang/include/clang/Sema/Sema.h | 12 + clang/lib/Sema/Sema.cpp | 25 + clang/lib/Sema/SemaDecl.cpp | 6 +++ clang/lib/Sema/SemaStmt.cpp | 6 +++ clang/test/Sema/fdump_auto-type-inference.cpp | 51 +++ 6 files changed, 104 insertions(+) create mode 100644 clang/test/Sema/fdump_auto-type-inference.cpp diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index f04c220d6e1db..655bfcd66e970 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -7467,6 +7467,10 @@ def ast_dump_filter : Separate<["-"], "ast-dump-filter">, MarshallingInfoString>; def ast_dump_filter_EQ : Joined<["-"], "ast-dump-filter=">, Alias; +def fdump_auto_type_inference : Flag<["-"], "fdump-auto-type-inference">, Group, +HelpTex