[clang] [Clang] Optimize some `getBeginLoc` implementations (PR #141058)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Thu May 22 06:02:10 PDT 2025


================
@@ -3187,9 +3216,59 @@ class CallExpr : public Expr {
   SourceLocation getRParenLoc() const { return RParenLoc; }
   void setRParenLoc(SourceLocation L) { RParenLoc = L; }
 
-  SourceLocation getBeginLoc() const LLVM_READONLY;
-  SourceLocation getEndLoc() const LLVM_READONLY;
+  template <unsigned N> SourceLocation getTrailingSourceLoc() const {
+    static_assert(N <= 1);
+    assert(CallExprBits.HasTrailingSourceLocs && "No trailing source loc");
+    static_assert(sizeof(CallExpr) <=
+                  offsetToTrailingObjects + 2 * sizeof(SourceLocation));
+    return *reinterpret_cast<const SourceLocation *>(
+        reinterpret_cast<const char *>(this) + sizeof(CallExpr) +
+        sizeof(SourceLocation) * N);
+  }
+
+  SourceLocation getBeginLoc() const {
+    if (CallExprBits.HasTrailingSourceLocs)
+      return getTrailingSourceLoc<0>();
+
+    if (usesMemberSyntax()) {
+      if (auto FirstArgLoc = getArg(0)->getBeginLoc(); FirstArgLoc.isValid()) {
+        return FirstArgLoc;
+      }
+    }
+    return getCallee()->getBeginLoc();
+  }
+
+  SourceLocation getEndLoc() const {
+    if (CallExprBits.HasTrailingSourceLocs)
+      return getTrailingSourceLoc<0>();
+
+    SourceLocation end = getRParenLoc();
----------------
AaronBallman wrote:

```suggestion
    SourceLocation End = getRParenLoc();
```

https://github.com/llvm/llvm-project/pull/141058


More information about the cfe-commits mailing list