Skip to content

Dump auto (c++11) type Inferences (Issue #17) #144478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

Shrinidhi-I
Copy link

Dump auto (c++11) type Inferences

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@Shrinidhi-I
Copy link
Author

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.

Compilation Command (from build directory)
./bin/clang++ -mllvm -fdump-auto-type-inference ./Hello.cpp

Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff HEAD~1 HEAD --extensions h,cpp -- clang/test/Sema/fdump_auto-type-inference.cpp clang/include/clang/Basic/LangOptions.h clang/include/clang/Sema/Sema.h clang/lib/Sema/Sema.cpp clang/lib/Sema/SemaDecl.cpp clang/lib/Sema/SemaStmt.cpp
View the diff from clang-format here.
diff --git a/clang/include/clang/Basic/LangOptions.h b/clang/include/clang/Basic/LangOptions.h
index f80ccb7ee..aee773d09 100644
--- a/clang/include/clang/Basic/LangOptions.h
+++ b/clang/include/clang/Basic/LangOptions.h
@@ -510,7 +510,7 @@ public:
   SanitizerSet Sanitize;
   /// Is at least one coverage instrumentation type enabled.
   bool SanitizeCoverage = false;
-  
+
   bool DumpAutoTypeInference = false;
   /// Paths to files specifying which objects
   /// (files, functions, variables) should not be instrumented.
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index c955766cf..781f0a509 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -89,10 +89,10 @@
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/TinyPtrVector.h"
 #include "llvm/Support/Allocator.h"
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/ErrorHandling.h"
-#include "llvm/Support/CommandLine.h"
 #include <cassert>
 #include <climits>
 #include <cstddef>
@@ -108,15 +108,12 @@
 #include <utility>
 #include <vector>
 
-
-
 namespace opts {
 // Option for dumping auto type inference
 extern llvm::cl::OptionCategory DumpAutoInference;
 extern llvm::cl::opt<bool> DumpAutoTypeInference;
 } // namespace opts
 
-
 namespace llvm {
 struct InlineAsmIdentifierInfo;
 } // namespace llvm
@@ -933,14 +930,12 @@ public:
   /// Print out statistics about the semantic analysis.
   void PrintStats() const;
 
-    /// Emits diagnostic remark indicating the compiler-deduced types and return
+  /// Emits diagnostic remark indicating the compiler-deduced types and return
   /// type for variables and functions
   void DumpAutoTypeInference(SourceManager &SM, SourceLocation Loc, bool isVar,
                              ASTContext &Context, llvm::StringRef Name,
                              QualType DeducedType);
 
-
-
   /// Run some code with "sufficient" stack space. (Currently, at least 256K is
   /// guaranteed). Produces a warning if we're low on stack space and allocates
   /// more in that case. Use this in code that may recurse deeply (for example,
diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp
index 707d7c0d5..e735b2529 100644
--- a/clang/lib/Sema/Sema.cpp
+++ b/clang/lib/Sema/Sema.cpp
@@ -84,8 +84,9 @@ namespace opts {
 llvm::cl::OptionCategory DumpAutoInference("DumpAutoInference");
 llvm::cl::opt<bool> DumpAutoTypeInference{
     "fdump-auto-type-inference",
-    llvm::cl::desc("Dump compiler-deduced type for variables and return expressions declared using C++ 'auto' keyword"), llvm::cl::ZeroOrMore,
-    llvm::cl::cat(DumpAutoInference)};
+    llvm::cl::desc("Dump compiler-deduced type for variables and return "
+                   "expressions declared using C++ 'auto' keyword"),
+    llvm::cl::ZeroOrMore, llvm::cl::cat(DumpAutoInference)};
 } // namespace opts
 
 SourceLocation Sema::getLocForEndOfToken(SourceLocation Loc, unsigned Offset) {
@@ -651,8 +652,6 @@ void Sema::DumpAutoTypeInference(SourceManager &SM, SourceLocation Loc,
   }
 }
 
-
-
 void Sema::runWithSufficientStackSpace(SourceLocation Loc,
                                        llvm::function_ref<void()> Fn) {
   StackHandler.runWithSufficientStackSpace(Loc, Fn);
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index de3a662d6..643135b10 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -13159,7 +13159,7 @@ bool Sema::DeduceVariableDeclarationType(VarDecl *VDecl, bool DirectInit,
 
   VDecl->setType(DeducedType);
   assert(VDecl->isLinkageValid());
-  
+
   // Emit a remark indicating the compiler-deduced type for variables declared
   // using the C++ 'auto' keyword
   SourceManager &SM = getSourceManager();
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index a5406d279..796e81a58 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -3834,7 +3834,7 @@ bool Sema::DeduceFunctionTypeFromReturnExpr(FunctionDecl *FD,
     // Update all declarations of the function to have the deduced return type.
     Context.adjustDeducedFunctionResultType(FD, Deduced);
 
-      // Emit a remark indicating the compiler-deduced return type for functions
+  // Emit a remark indicating the compiler-deduced return type for functions
   // declared using the C++ 'auto' keyword
   SourceManager &SM = getSourceManager();
   Sema::DumpAutoTypeInference(SM, FD->getLocation(), false, Context,

@zwuis
Copy link
Contributor

zwuis commented Jun 17, 2025

Is it necessary to have this feature? Clangd already supports it.

@zwuis zwuis requested a review from AaronBallman June 17, 2025 17:21
@AaronBallman
Copy link
Collaborator

First off: thank you for the patch and trying to improve Clang!

Is it necessary to have this feature? Clangd already supports it.

Also, Clang itself already has the ability to dump information about the AST: https://godbolt.org/z/aEM53YKq9

`-VarDecl 0x1be884d8 <<source>:1:1, col:10> col:6 x 'int' cinit
  `-IntegerLiteral 0x1be88588 <col:10> 'int' 12

(and has ways to filter AST dumps so there's less information about unrelated nodes), so I also question whether this functionality is necessary under another flag. I think the existing facilities already suffice, but maybe I'm missing something?

@zwuis
Copy link
Contributor

zwuis commented Jun 19, 2025

Copying a comment from #144622:

Please start a thread on https://discourse.llvm.org/ describing the problem you're trying to solve, and your proposed solution.

@zwuis
Copy link
Contributor

zwuis commented Jun 20, 2025

You should fork https://github.com/Ritanya-B-Bharadwaj/llvm-project and create Pull Request there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants