[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();
+    if (end.isInvalid() && getNumArgs() > 0 && getArg(getNumArgs() - 1))
+      end = getArg(getNumArgs() - 1)->getEndLoc();
+    return end;
+  }
+
+private:
+  bool hasTrailingSourceLoc() const {
+    return CallExprBits.HasTrailingSourceLocs;
+  }
 
+  void updateTrailingSourceLocs() {
+    assert(!CallExprBits.HasTrailingSourceLocs &&
+           "Trailing source loc already set?");
+    assert(getStmtClass() == CallExprClass &&
+           "Calling setTrailingSourceLocs on a subclass of CallExpr");
+    static_assert(sizeof(CallExpr) <=
+                  offsetToTrailingObjects + 2 * sizeof(SourceLocation));
+
+    SourceLocation *Locs = reinterpret_cast<SourceLocation *>(
+        reinterpret_cast<char *>(this) + sizeof(CallExpr));
----------------
AaronBallman wrote:

`reinterpret_cast<SourceLocation *>(this + 1)` ?

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


More information about the cfe-commits mailing list