[clang] 5522e31 - [clang][NFC] Simplify SourceLocExpr::EvaluateInContext

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


Author: Timm Bäder
Date: 2023-07-20T15:15:57+02:00
New Revision: 5522e316f91ef490b34ffdf2c21fc8fed8affe5f

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

LOG: [clang][NFC] Simplify SourceLocExpr::EvaluateInContext

Use ASTContext::MakeIntValue and remove the std::tie+lambda weirdness.

Differential Revision: https://reviews.llvm.org/D155584

Added: 
    

Modified: 
    clang/lib/AST/Expr.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 25d3535e5dac43..23aa14784cdaab 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -2288,14 +2288,17 @@ APValue SourceLocExpr::EvaluateInContext(const ASTContext &Ctx,
   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 @@ APValue SourceLocExpr::EvaluateInContext(const ASTContext &Ctx,
         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 @@ APValue SourceLocExpr::EvaluateInContext(const ASTContext &Ctx,
     // 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 @@ APValue SourceLocExpr::EvaluateInContext(const ASTContext &Ctx,
                       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);
       }
     }


        


More information about the cfe-commits mailing list