[clang] Issue #19 and #20 - Describe auto (C++11) type inferences (PR #86474)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 25 01:09:54 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Nisarga V (nisarga3)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/86474.diff
7 Files Affected:
- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+2)
- (modified) clang/include/clang/Basic/LangOptions.def (+1)
- (modified) clang/include/clang/Driver/Options.td (+6)
- (modified) clang/include/clang/Frontend/FrontendOptions.h (+6)
- (modified) clang/lib/Frontend/CompilerInvocation.cpp (+7)
- (modified) clang/lib/Sema/SemaDecl.cpp (+8)
- (modified) clang/tools/driver/cc1_main.cpp (+4)
``````````diff
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/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/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 b5c6be3c557bb3..065c8b8689e979 100644
--- a/clang/tools/driver/cc1_main.cpp
+++ b/clang/tools/driver/cc1_main.cpp
@@ -191,6 +191,10 @@ 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";
+ }
if (!Clang->getFrontendOpts().TimeTracePath.empty()) {
llvm::timeTraceProfilerInitialize(
``````````
</details>
https://github.com/llvm/llvm-project/pull/86474
More information about the cfe-commits
mailing list