Author: Timm Bäder Date: 2023-01-25T12:52:44+01:00 New Revision: f4a6842c5a4d729bacfd0240b44fa726a28e6bfb
URL: https://github.com/llvm/llvm-project/commit/f4a6842c5a4d729bacfd0240b44fa726a28e6bfb DIFF: https://github.com/llvm/llvm-project/commit/f4a6842c5a4d729bacfd0240b44fa726a28e6bfb.diff LOG: [clang][Interp] Reject invalid declarations and expressions Reject them early, since we will run into problems and/or assertions later on anyway. Differential Revision: https://reviews.llvm.org/D137386 Added: Modified: clang/lib/AST/Interp/ByteCodeExprGen.cpp clang/test/AST/Interp/functions.cpp Removed: ################################################################################ diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp index 5952e3aa681c..d28b89381b87 100644 --- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp +++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp @@ -588,12 +588,18 @@ bool ByteCodeExprGen<Emitter>::VisitCompoundAssignOperator( } template <class Emitter> bool ByteCodeExprGen<Emitter>::discard(const Expr *E) { + if (E->containsErrors()) + return false; + OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/true); return this->Visit(E); } template <class Emitter> bool ByteCodeExprGen<Emitter>::visit(const Expr *E) { + if (E->containsErrors()) + return false; + OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false); return this->Visit(E); } @@ -1156,6 +1162,7 @@ bool ByteCodeExprGen<Emitter>::visitExpr(const Expr *Exp) { /// We need to evaluate the initializer and return its value. template <class Emitter> bool ByteCodeExprGen<Emitter>::visitDecl(const VarDecl *VD) { + assert(!VD->isInvalidDecl() && "Trying to constant evaluate an invalid decl"); std::optional<PrimType> VarT = classify(VD->getType()); // Create and initialize the variable. diff --git a/clang/test/AST/Interp/functions.cpp b/clang/test/AST/Interp/functions.cpp index e372f23987e2..4fb5d3d98d74 100644 --- a/clang/test/AST/Interp/functions.cpp +++ b/clang/test/AST/Interp/functions.cpp @@ -84,3 +84,18 @@ constexpr int f() { return 5; } static_assert(a() == 5, ""); + +constexpr int invalid() { + // Invalid expression in visit(). + while(huh) {} // expected-error {{use of undeclared identifier}} \ + // ref-error {{use of undeclared identifier}} + + return 0; +} + +constexpr void invalid2() { + int i = 0; + // Invalid expression in discard(). + huh(); // expected-error {{use of undeclared identifier}} \ + // ref-error {{use of undeclared identifier}} +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits