[clang] 7a3ad8e - [clang] Provide source range to 'invalid subexpr in const expr' diags
Timm Bäder via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 26 00:12:25 PDT 2023
Author: Timm Bäder
Date: 2023-07-26T09:12:11+02:00
New Revision: 7a3ad8ed77eef2e9ac218dd5161dda5fab1238b6
URL: https://github.com/llvm/llvm-project/commit/7a3ad8ed77eef2e9ac218dd5161dda5fab1238b6
DIFF: https://github.com/llvm/llvm-project/commit/7a3ad8ed77eef2e9ac218dd5161dda5fab1238b6.diff
LOG: [clang] Provide source range to 'invalid subexpr in const expr' diags
Differential Revision: https://reviews.llvm.org/D150566
Added:
clang/test/Misc/constexpr-source-ranges.cpp
Modified:
clang/lib/AST/ExprConstant.cpp
clang/lib/AST/Interp/Interp.h
clang/lib/AST/Interp/InterpFrame.cpp
clang/lib/AST/Interp/InterpFrame.h
clang/lib/AST/Interp/Source.cpp
clang/lib/AST/Interp/Source.h
Removed:
################################################################################
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index f1c842e261993d..755e71f6011d6c 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -5227,7 +5227,7 @@ static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info,
return ESR_Succeeded;
}
- Info.FFDiag(S->getBeginLoc());
+ Info.FFDiag(S->getBeginLoc()) << S->getSourceRange();
return ESR_Failed;
case Stmt::NullStmtClass:
@@ -7454,7 +7454,7 @@ class ExprEvaluatorBase
/// Report an evaluation error. This should only be called when an error is
/// first discovered. When propagating an error, just return false.
bool Error(const Expr *E, diag::kind D) {
- Info.FFDiag(E, D);
+ Info.FFDiag(E, D) << E->getSourceRange();
return false;
}
bool Error(const Expr *E) {
diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h
index 8a17322ba6ca55..e0cbf56e196958 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -1738,7 +1738,8 @@ inline bool GetFnPtr(InterpState &S, CodePtr OpPC, const Function *Func) {
/// op is not valid in a constant context.
inline bool Invalid(InterpState &S, CodePtr OpPC) {
const SourceLocation &Loc = S.Current->getLocation(OpPC);
- S.FFDiag(Loc, diag::note_invalid_subexpr_in_const_expr);
+ S.FFDiag(Loc, diag::note_invalid_subexpr_in_const_expr)
+ << S.Current->getRange(OpPC);
return false;
}
diff --git a/clang/lib/AST/Interp/InterpFrame.cpp b/clang/lib/AST/Interp/InterpFrame.cpp
index 2229aa7c08f6b2..23a98543bdc3f8 100644
--- a/clang/lib/AST/Interp/InterpFrame.cpp
+++ b/clang/lib/AST/Interp/InterpFrame.cpp
@@ -229,3 +229,6 @@ SourceLocation InterpFrame::getLocation(CodePtr PC) const {
return S.getLocation(Func, PC);
}
+SourceRange InterpFrame::getRange(CodePtr PC) const {
+ return S.getRange(Func, PC);
+}
diff --git a/clang/lib/AST/Interp/InterpFrame.h b/clang/lib/AST/Interp/InterpFrame.h
index ce58fb8d3f84eb..c2bce93a1e8f66 100644
--- a/clang/lib/AST/Interp/InterpFrame.h
+++ b/clang/lib/AST/Interp/InterpFrame.h
@@ -118,6 +118,7 @@ class InterpFrame final : public Frame {
virtual SourceInfo getSource(CodePtr PC) const;
const Expr *getExpr(CodePtr PC) const;
SourceLocation getLocation(CodePtr PC) const;
+ SourceRange getRange(CodePtr PC) const;
unsigned getDepth() const { return Depth; }
diff --git a/clang/lib/AST/Interp/Source.cpp b/clang/lib/AST/Interp/Source.cpp
index 467cde116843c6..4e032c92d26df1 100644
--- a/clang/lib/AST/Interp/Source.cpp
+++ b/clang/lib/AST/Interp/Source.cpp
@@ -22,6 +22,16 @@ SourceLocation SourceInfo::getLoc() const {
return SourceLocation();
}
+SourceRange SourceInfo::getRange() const {
+ if (const Expr *E = asExpr())
+ return E->getSourceRange();
+ if (const Stmt *S = asStmt())
+ return S->getSourceRange();
+ if (const Decl *D = asDecl())
+ return D->getSourceRange();
+ return SourceRange();
+}
+
const Expr *SourceInfo::asExpr() const {
if (auto *S = Source.dyn_cast<const Stmt *>())
return dyn_cast<Expr>(S);
@@ -37,3 +47,7 @@ const Expr *SourceMapper::getExpr(const Function *F, CodePtr PC) const {
SourceLocation SourceMapper::getLocation(const Function *F, CodePtr PC) const {
return getSource(F, PC).getLoc();
}
+
+SourceRange SourceMapper::getRange(const Function *F, CodePtr PC) const {
+ return getSource(F, PC).getRange();
+}
diff --git a/clang/lib/AST/Interp/Source.h b/clang/lib/AST/Interp/Source.h
index 89fca9ac80f289..6ffc7763587747 100644
--- a/clang/lib/AST/Interp/Source.h
+++ b/clang/lib/AST/Interp/Source.h
@@ -71,6 +71,7 @@ class SourceInfo final {
SourceInfo(const Decl *D) : Source(D) {}
SourceLocation getLoc() const;
+ SourceRange getRange() const;
const Stmt *asStmt() const { return Source.dyn_cast<const Stmt *>(); }
const Decl *asDecl() const { return Source.dyn_cast<const Decl *>(); }
@@ -96,6 +97,7 @@ class SourceMapper {
const Expr *getExpr(const Function *F, CodePtr PC) const;
/// Returns the location from which an opcode originates.
SourceLocation getLocation(const Function *F, CodePtr PC) const;
+ SourceRange getRange(const Function *F, CodePtr PC) const;
};
} // namespace interp
diff --git a/clang/test/Misc/constexpr-source-ranges.cpp b/clang/test/Misc/constexpr-source-ranges.cpp
new file mode 100644
index 00000000000000..9ab6cc49200166
--- /dev/null
+++ b/clang/test/Misc/constexpr-source-ranges.cpp
@@ -0,0 +1,9 @@
+// RUN: not %clang_cc1 -fsyntax-only -fdiagnostics-print-source-range-info -fcxx-exceptions %s 2>&1 | FileCheck %s
+// RUN: not %clang_cc1 -fsyntax-only -fexperimental-new-constant-interpreter -fdiagnostics-print-source-range-info -fcxx-exceptions %s 2>&1 | FileCheck %s
+
+constexpr int f() {
+ throw 1;
+ return 0;
+}
+
+// CHECK: constexpr-source-ranges.cpp:5:3:{5:3-5:10}
More information about the cfe-commits
mailing list