[clang] e5936b2 - [clang][Interp][NFC] Fix toAPValue() for array root pointers

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Wed May 22 03:24:59 PDT 2024


Author: Timm Bäder
Date: 2024-05-22T12:14:56+02:00
New Revision: e5936b245e9af0cea69a7e4eae22a05b7ffcf5a3

URL: https://github.com/llvm/llvm-project/commit/e5936b245e9af0cea69a7e4eae22a05b7ffcf5a3
DIFF: https://github.com/llvm/llvm-project/commit/e5936b245e9af0cea69a7e4eae22a05b7ffcf5a3.diff

LOG: [clang][Interp][NFC] Fix toAPValue() for array root pointers

isArrayElement() returns false for them, so we used to add the decl
to the path, causing wrong APValues to be generated.

Added: 
    

Modified: 
    clang/lib/AST/Interp/Pointer.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/Pointer.cpp b/clang/lib/AST/Interp/Pointer.cpp
index ee8cedccb8d4b..e45c291668caf 100644
--- a/clang/lib/AST/Interp/Pointer.cpp
+++ b/clang/lib/AST/Interp/Pointer.cpp
@@ -144,13 +144,18 @@ APValue Pointer::toAPValue() const {
 
   // TODO: compute the offset into the object.
   CharUnits Offset = CharUnits::Zero();
-  bool IsOnePastEnd = isOnePastEnd();
 
   // Build the path into the object.
   Pointer Ptr = *this;
   while (Ptr.isField() || Ptr.isArrayElement()) {
-    if (Ptr.isArrayElement()) {
-      Path.push_back(APValue::LValuePathEntry::ArrayIndex(Ptr.getIndex()));
+    if (Ptr.isArrayRoot()) {
+        Path.push_back(APValue::LValuePathEntry::ArrayIndex(0));
+        Ptr = Ptr.getBase();
+    } else if (Ptr.isArrayElement()) {
+      if (Ptr.isOnePastEnd())
+        Path.push_back(APValue::LValuePathEntry::ArrayIndex(Ptr.getArray().getNumElems()));
+      else
+        Path.push_back(APValue::LValuePathEntry::ArrayIndex(Ptr.getIndex()));
       Ptr = Ptr.getArray();
     } else {
       // TODO: figure out if base is virtual
@@ -173,7 +178,7 @@ APValue Pointer::toAPValue() const {
   // Just invert the order of the elements.
   std::reverse(Path.begin(), Path.end());
 
-  return APValue(Base, Offset, Path, IsOnePastEnd, /*IsNullPtr=*/false);
+  return APValue(Base, Offset, Path, /*IsOnePastEnd=*/false, /*IsNullPtr=*/false);
 }
 
 void Pointer::print(llvm::raw_ostream &OS) const {


        


More information about the cfe-commits mailing list