[clang] [clang][bytecode] SourceInfo::Source might be null (PR #115905)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 12 09:02:14 PST 2024


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/115905

This broke in 23fbaff9a3fd2b26418e0c2f10b701049399251f, but the old .dyn_cast<> handled null.

>From 1040853e20625745112a1b0160c628021b62ad59 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 12 Nov 2024 17:58:35 +0100
Subject: [PATCH] [clang][bytecode] SourceInfo::Source might be null

This broke in 23fbaff9a3fd2b26418e0c2f10b701049399251f, but the
old .dyn_cast<> handled null.
---
 clang/lib/AST/ByteCode/Source.cpp         | 2 +-
 clang/lib/AST/ByteCode/Source.h           | 8 ++++++--
 clang/test/SemaCXX/lambda-expressions.cpp | 5 +++++
 3 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Source.cpp b/clang/lib/AST/ByteCode/Source.cpp
index 55296ddd8c5583..d4ce7537f3d244 100644
--- a/clang/lib/AST/ByteCode/Source.cpp
+++ b/clang/lib/AST/ByteCode/Source.cpp
@@ -33,7 +33,7 @@ SourceRange SourceInfo::getRange() const {
 }
 
 const Expr *SourceInfo::asExpr() const {
-  if (const auto *S = dyn_cast<const Stmt *>(Source))
+  if (const auto *S = dyn_cast_if_present<const Stmt *>(Source))
     return dyn_cast<Expr>(S);
   return nullptr;
 }
diff --git a/clang/lib/AST/ByteCode/Source.h b/clang/lib/AST/ByteCode/Source.h
index 3b025535d00b19..c74cda919347c0 100644
--- a/clang/lib/AST/ByteCode/Source.h
+++ b/clang/lib/AST/ByteCode/Source.h
@@ -83,8 +83,12 @@ class SourceInfo final {
   SourceLocation getLoc() const;
   SourceRange getRange() const;
 
-  const Stmt *asStmt() const { return dyn_cast<const Stmt *>(Source); }
-  const Decl *asDecl() const { return dyn_cast<const Decl *>(Source); }
+  const Stmt *asStmt() const {
+    return dyn_cast_if_present<const Stmt *>(Source);
+  }
+  const Decl *asDecl() const {
+    return dyn_cast_if_present<const Decl *>(Source);
+  }
   const Expr *asExpr() const;
 
   operator bool() const { return !Source.isNull(); }
diff --git a/clang/test/SemaCXX/lambda-expressions.cpp b/clang/test/SemaCXX/lambda-expressions.cpp
index acf8d014a9896b..f3deb6ee3f4244 100644
--- a/clang/test/SemaCXX/lambda-expressions.cpp
+++ b/clang/test/SemaCXX/lambda-expressions.cpp
@@ -3,6 +3,11 @@
 // RUN: %clang_cc1 -std=c++14 -Wno-unused-value -fsyntax-only -verify=expected,not-cxx03,expected-cxx14 -fblocks %s
 // RUN: %clang_cc1 -std=c++17 -Wno-unused-value -verify=expected,not-cxx03 -ast-dump -fblocks %s | FileCheck %s
 
+// RUN: %clang_cc1 -std=c++11 -Wno-unused-value -fsyntax-only -verify=expected,not-cxx03,cxx03-cxx11,cxx11,expected-cxx14 -fblocks %s -fexperimental-new-constant-interpreter
+// RUN: %clang_cc1 -std=c++03 -Wno-unused-value -fsyntax-only -verify=expected,cxx03,cxx03-cxx11,expected-cxx14 -fblocks %s -Ddecltype=__decltype -Dstatic_assert=_Static_assert -Wno-c++11-extensions -fexperimental-new-constant-interpreter
+// RUN: %clang_cc1 -std=c++14 -Wno-unused-value -fsyntax-only -verify=expected,not-cxx03,expected-cxx14 -fblocks %s -fexperimental-new-constant-interpreter
+// RUN: %clang_cc1 -std=c++17 -Wno-unused-value -verify=expected,not-cxx03 -ast-dump -fblocks %s -fexperimental-new-constant-interpreter| FileCheck %s
+
 namespace std { class type_info; };
 
 namespace ExplicitCapture {



More information about the cfe-commits mailing list