tbaeder created this revision.
tbaeder added reviewers: cjdb, aaron.ballman.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D150566

Files:
  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
  clang/test/Misc/constexpr-source-ranges.cpp

Index: clang/test/Misc/constexpr-source-ranges.cpp
===================================================================
--- /dev/null
+++ 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}
Index: clang/lib/AST/Interp/Source.h
===================================================================
--- clang/lib/AST/Interp/Source.h
+++ clang/lib/AST/Interp/Source.h
@@ -74,6 +74,7 @@
   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 *>(); }
@@ -99,6 +100,7 @@
   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
Index: clang/lib/AST/Interp/Source.cpp
===================================================================
--- clang/lib/AST/Interp/Source.cpp
+++ clang/lib/AST/Interp/Source.cpp
@@ -22,6 +22,16 @@
   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 @@
 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();
+}
Index: clang/lib/AST/Interp/InterpFrame.h
===================================================================
--- clang/lib/AST/Interp/InterpFrame.h
+++ clang/lib/AST/Interp/InterpFrame.h
@@ -118,6 +118,7 @@
   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; }
 
Index: clang/lib/AST/Interp/InterpFrame.cpp
===================================================================
--- clang/lib/AST/Interp/InterpFrame.cpp
+++ clang/lib/AST/Interp/InterpFrame.cpp
@@ -225,3 +225,6 @@
   return S.getLocation(Func, PC);
 }
 
+SourceRange InterpFrame::getRange(CodePtr PC) const {
+  return S.getRange(Func, PC);
+}
Index: clang/lib/AST/Interp/Interp.h
===================================================================
--- clang/lib/AST/Interp/Interp.h
+++ clang/lib/AST/Interp/Interp.h
@@ -1827,7 +1827,8 @@
 /// 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;
 }
 
Index: clang/lib/AST/ExprConstant.cpp
===================================================================
--- clang/lib/AST/ExprConstant.cpp
+++ clang/lib/AST/ExprConstant.cpp
@@ -5201,7 +5201,7 @@
       return ESR_Succeeded;
     }
 
-    Info.FFDiag(S->getBeginLoc());
+    Info.FFDiag(S->getBeginLoc()) << S->getSourceRange();
     return ESR_Failed;
 
   case Stmt::NullStmtClass:
@@ -7427,7 +7427,7 @@
   /// 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) {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D150566: [clang] Pro... Timm Bäder via Phabricator via cfe-commits

Reply via email to