-
Notifications
You must be signed in to change notification settings - Fork 14.5k
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
Conversation
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 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. |
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) |
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,
|
Is it necessary to have this feature? Clangd already supports it. |
First off: thank you for the patch and trying to improve Clang!
Also, Clang itself already has the ability to dump information about the AST: https://godbolt.org/z/aEM53YKq9
(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? |
Copying a comment from #144622:
|
You should fork https://github.com/Ritanya-B-Bharadwaj/llvm-project and create Pull Request there. |
Dump auto (c++11) type Inferences