From: Daniel Dunbar Date: Fri, 9 Mar 2012 15:39:19 +0000 (+0000) Subject: [AST] Reimplement Stmt::getLoc{Start,End} to dispatch to subclass overloads. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=90e25a8b2cc5d006e4ced052bcdb40c8af999456;p=clang [AST] Reimplement Stmt::getLoc{Start,End} to dispatch to subclass overloads. - getSourceRange() can be very expensive, we should try to avoid it if at all possible. In conjunction with the previous commit I measured a ~2% speedup on 403.gcc/combine.c and a 3% speedup on OmniGroupFrameworks/NSBezierPath-OAExtensions.m. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@152411 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/Stmt.h b/include/clang/AST/Stmt.h index 7ef3e2b2ec..d674344377 100644 --- a/include/clang/AST/Stmt.h +++ b/include/clang/AST/Stmt.h @@ -348,8 +348,8 @@ public: /// clients will have a pointer to the respective SourceManager. SourceRange getSourceRange() const; - SourceLocation getLocStart() const { return getSourceRange().getBegin(); } - SourceLocation getLocEnd() const { return getSourceRange().getEnd(); } + SourceLocation getLocStart() const; + SourceLocation getLocEnd() const; // global temp stats (until we have a per-module visitor) static void addStmtClass(const StmtClass s); diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp index 0266cd63bb..8e2e64faf1 100644 --- a/lib/AST/Expr.cpp +++ b/lib/AST/Expr.cpp @@ -94,6 +94,8 @@ bool Expr::isKnownToHaveBooleanValue() const { // Amusing macro metaprogramming hack: check whether a class provides // a more specific implementation of getExprLoc(). +// +// See also Stmt.cpp:{getLocStart(),getLocEnd()}. namespace { /// This implementation is used when a class provides a custom /// implementation of getExprLoc. @@ -110,7 +112,7 @@ namespace { template SourceLocation getExprLocImpl(const Expr *expr, SourceLocation (Expr::*v)() const) { - return static_cast(expr)->getSourceRange().getBegin(); + return static_cast(expr)->getLocStart(); } } diff --git a/lib/AST/Stmt.cpp b/lib/AST/Stmt.cpp index 0deaaa83ba..7907e565dd 100644 --- a/lib/AST/Stmt.cpp +++ b/lib/AST/Stmt.cpp @@ -176,6 +176,72 @@ SourceRange Stmt::getSourceRange() const { llvm_unreachable("unknown statement kind!"); } +// Amusing macro metaprogramming hack: check whether a class provides +// a more specific implementation of getLocStart() and getLocEnd(). +// +// See also Expr.cpp:getExprLoc(). +namespace { + /// This implementation is used when a class provides a custom + /// implementation of getLocStart. + template + SourceLocation getLocStartImpl(const Stmt *stmt, + SourceLocation (T::*v)() const) { + return static_cast(stmt)->getLocStart(); + } + + /// This implementation is used when a class doesn't provide a custom + /// implementation of getLocStart. Overload resolution should pick it over + /// the implementation above because it's more specialized according to + /// function template partial ordering. + template + SourceLocation getLocStartImpl(const Stmt *stmt, + SourceLocation (Stmt::*v)() const) { + return static_cast(stmt)->getSourceRange().getBegin(); + } + + /// This implementation is used when a class provides a custom + /// implementation of getLocEnd. + template + SourceLocation getLocEndImpl(const Stmt *stmt, + SourceLocation (T::*v)() const) { + return static_cast(stmt)->getLocEnd(); + } + + /// This implementation is used when a class doesn't provide a custom + /// implementation of getLocEnd. Overload resolution should pick it over + /// the implementation above because it's more specialized according to + /// function template partial ordering. + template + SourceLocation getLocEndImpl(const Stmt *stmt, + SourceLocation (Stmt::*v)() const) { + return static_cast(stmt)->getSourceRange().getEnd(); + } +} + +SourceLocation Stmt::getLocStart() const { + switch (getStmtClass()) { + case Stmt::NoStmtClass: llvm_unreachable("statement without class"); +#define ABSTRACT_STMT(type) +#define STMT(type, base) \ + case Stmt::type##Class: \ + return getLocStartImpl(this, &type::getLocStart); +#include "clang/AST/StmtNodes.inc" + } + llvm_unreachable("unknown statement kind"); +} + +SourceLocation Stmt::getLocEnd() const { + switch (getStmtClass()) { + case Stmt::NoStmtClass: llvm_unreachable("statement without class"); +#define ABSTRACT_STMT(type) +#define STMT(type, base) \ + case Stmt::type##Class: \ + return getLocEndImpl(this, &type::getLocEnd); +#include "clang/AST/StmtNodes.inc" + } + llvm_unreachable("unknown statement kind"); +} + void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) { if (this->Body) C.Deallocate(Body);