[PATCH] D155584: [clang][NFC] Simplify SourceLocExpr::EvaluateInContext

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 20 06:21:06 PDT 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rG5522e316f91e: [clang][NFC] Simplify SourceLocExpr::EvaluateInContext (authored by tbaeder).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155584/new/

https://reviews.llvm.org/D155584

Files:
  clang/lib/AST/Expr.cpp


Index: clang/lib/AST/Expr.cpp
===================================================================
--- clang/lib/AST/Expr.cpp
+++ clang/lib/AST/Expr.cpp
@@ -2288,14 +2288,17 @@
   SourceLocation Loc;
   const DeclContext *Context;
 
-  std::tie(Loc,
-           Context) = [&]() -> std::pair<SourceLocation, const DeclContext *> {
-    if (auto *DIE = dyn_cast_or_null<CXXDefaultInitExpr>(DefaultExpr))
-      return {DIE->getUsedLocation(), DIE->getUsedContext()};
-    if (auto *DAE = dyn_cast_or_null<CXXDefaultArgExpr>(DefaultExpr))
-      return {DAE->getUsedLocation(), DAE->getUsedContext()};
-    return {this->getLocation(), this->getParentContext()};
-  }();
+  if (const auto *DIE = dyn_cast_if_present<CXXDefaultInitExpr>(DefaultExpr)) {
+    Loc = DIE->getUsedLocation();
+    Context = DIE->getUsedContext();
+  } else if (const auto *DAE =
+                 dyn_cast_if_present<CXXDefaultArgExpr>(DefaultExpr)) {
+    Loc = DAE->getUsedLocation();
+    Context = DAE->getUsedContext();
+  } else {
+    Loc = getLocation();
+    Context = getParentContext();
+  }
 
   PresumedLoc PLoc = Ctx.getSourceManager().getPresumedLoc(
       Ctx.getSourceManager().getExpansionRange(Loc).getEnd());
@@ -2333,13 +2336,9 @@
         CurDecl ? PredefinedExpr::ComputeName(Kind, CurDecl) : std::string(""));
   }
   case SourceLocExpr::Line:
-  case SourceLocExpr::Column: {
-    llvm::APSInt IntVal(Ctx.getIntWidth(Ctx.UnsignedIntTy),
-                        /*isUnsigned=*/true);
-    IntVal = getIdentKind() == SourceLocExpr::Line ? PLoc.getLine()
-                                                   : PLoc.getColumn();
-    return APValue(IntVal);
-  }
+    return APValue(Ctx.MakeIntValue(PLoc.getLine(), Ctx.UnsignedIntTy));
+  case SourceLocExpr::Column:
+    return APValue(Ctx.MakeIntValue(PLoc.getColumn(), Ctx.UnsignedIntTy));
   case SourceLocExpr::SourceLocStruct: {
     // Fill in a std::source_location::__impl structure, by creating an
     // artificial file-scoped CompoundLiteralExpr, and returning a pointer to
@@ -2352,7 +2351,7 @@
     // the ImplDecl type is as expected.
 
     APValue Value(APValue::UninitStruct(), 0, 4);
-    for (FieldDecl *F : ImplDecl->fields()) {
+    for (const FieldDecl *F : ImplDecl->fields()) {
       StringRef Name = F->getName();
       if (Name == "_M_file_name") {
         SmallString<256> Path(PLoc.getFilename());
@@ -2369,16 +2368,10 @@
                       PredefinedExpr::PrettyFunction, CurDecl))
                 : "");
       } else if (Name == "_M_line") {
-        QualType Ty = F->getType();
-        llvm::APSInt IntVal(Ctx.getIntWidth(Ty),
-                            Ty->hasUnsignedIntegerRepresentation());
-        IntVal = PLoc.getLine();
+        llvm::APSInt IntVal = Ctx.MakeIntValue(PLoc.getLine(), F->getType());
         Value.getStructField(F->getFieldIndex()) = APValue(IntVal);
       } else if (Name == "_M_column") {
-        QualType Ty = F->getType();
-        llvm::APSInt IntVal(Ctx.getIntWidth(Ty),
-                            Ty->hasUnsignedIntegerRepresentation());
-        IntVal = PLoc.getColumn();
+        llvm::APSInt IntVal = Ctx.MakeIntValue(PLoc.getColumn(), F->getType());
         Value.getStructField(F->getFieldIndex()) = APValue(IntVal);
       }
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D155584.542485.patch
Type: text/x-patch
Size: 3270 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230720/8a500b0d/attachment.bin>


More information about the cfe-commits mailing list