Skip to content

[clang] Extend SourceLocation to 64 bits. #147292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ struct CognitiveComplexity final {
// https://sonarcloud.io/projects?languages=c%2Ccpp&size=5 we can estimate:
// value ~20 would result in no allocs for 98% of functions, ~12 for 96%, ~10
// for 91%, ~8 for 88%, ~6 for 84%, ~4 for 77%, ~2 for 64%, and ~1 for 37%.
static_assert(sizeof(Detail) <= 8,
static_assert(sizeof(Detail) <= 16,
"Since we use SmallVector to minimize the amount of "
"allocations, we also need to consider the price we pay for "
"that in terms of stack usage. "
"Thus, it is good to minimize the size of the Detail struct.");
SmallVector<Detail, DefaultLimit> Details; // 25 elements is 200 bytes.
SmallVector<Detail, DefaultLimit> Details; // 25 elements is 400 bytes.
// Yes, 25 is a magic number. This is the seemingly-sane default for the
// upper limit for function cognitive complexity. Thus it would make sense
// to avoid allocations for any function that does not violate the limit.
Expand Down
4 changes: 3 additions & 1 deletion clang-tools-extra/clangd/unittests/SourceCodeTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,9 @@ TEST(SourceCodeTests, isSpelledInSource) {
// FIXME: Should it return false on SourceLocation()? Does it matter?
EXPECT_TRUE(isSpelledInSource(SourceLocation(), SM));
EXPECT_FALSE(isSpelledInSource(
SourceLocation::getFromRawEncoding(SourceLocation::UIntTy(1 << 31)), SM));
SourceLocation::getFromRawEncoding(
SourceLocation::UIntTy(1ULL << (SourceLocation::Bits - 1))),
SM));
}

struct IncrementalTestStep {
Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -3356,6 +3356,8 @@ class ASTContext : public RefCountedBase<ASTContext> {
getTrivialTypeSourceInfo(QualType T,
SourceLocation Loc = SourceLocation()) const;

CXXOperatorSourceInfo *getCXXOperatorSourceInfo(SourceRange R) const;

/// Add a deallocation callback that will be invoked when the
/// ASTContext is destroyed.
///
Expand Down
10 changes: 3 additions & 7 deletions clang/include/clang/AST/DeclBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -1952,17 +1952,13 @@ class DeclContext {
friend class ObjCContainerDecl;
/// For the bits in DeclContextBitfields
LLVM_PREFERRED_TYPE(DeclContextBitfields)
uint32_t : NumDeclContextBits;
uint64_t : NumDeclContextBits;

// Not a bitfield but this saves space.
// Note that ObjCContainerDeclBitfields is full.
SourceLocation AtStart;
uint64_t AtStart : SourceLocation::Bits;
};

/// Number of inherited and non-inherited bits in ObjCContainerDeclBitfields.
/// Note that here we rely on the fact that SourceLocation is 32 bits
/// wide. We check this with the static_assert in the ctor of DeclContext.
enum { NumObjCContainerDeclBits = 64 };
enum { NumObjCContainerDeclBits = NumDeclContextBits + SourceLocation::Bits };

/// Stores the bits used by LinkageSpecDecl.
/// If modified NumLinkageSpecDeclBits and the accessor
Expand Down
6 changes: 4 additions & 2 deletions clang/include/clang/AST/DeclObjC.h
Original file line number Diff line number Diff line change
Expand Up @@ -1090,10 +1090,12 @@ class ObjCContainerDecl : public NamedDecl, public DeclContext {
/// Note, the superclass's properties are not included in the list.
virtual void collectPropertiesToImplement(PropertyMap &PM) const {}

SourceLocation getAtStartLoc() const { return ObjCContainerDeclBits.AtStart; }
SourceLocation getAtStartLoc() const {
return SourceLocation::getFromRawEncoding(ObjCContainerDeclBits.AtStart);
}

void setAtStartLoc(SourceLocation Loc) {
ObjCContainerDeclBits.AtStart = Loc;
ObjCContainerDeclBits.AtStart = Loc.getRawEncoding();
}

// Marks the end of the container.
Expand Down
46 changes: 22 additions & 24 deletions clang/include/clang/AST/DeclarationName.h
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,11 @@ class DeclarationNameTable {
DeclarationName getCXXLiteralOperatorName(const IdentifierInfo *II);
};

struct CXXOperatorSourceInfo {
SourceLocation BeginOpNameLoc;
SourceLocation EndOpNameLoc;
};

/// DeclarationNameLoc - Additional source/type location info
/// for a declaration name. Needs a DeclarationName in order
/// to be interpreted correctly.
Expand All @@ -698,8 +703,7 @@ class DeclarationNameLoc {

// The location (if any) of the operator keyword is stored elsewhere.
struct CXXOpName {
SourceLocation BeginOpNameLoc;
SourceLocation EndOpNameLoc;
CXXOperatorSourceInfo *OInfo;
};

// The location (if any) of the operator keyword is stored elsewhere.
Expand All @@ -719,11 +723,6 @@ class DeclarationNameLoc {

void setNamedTypeLoc(TypeSourceInfo *TInfo) { NamedType.TInfo = TInfo; }

void setCXXOperatorNameRange(SourceRange Range) {
CXXOperatorName.BeginOpNameLoc = Range.getBegin();
CXXOperatorName.EndOpNameLoc = Range.getEnd();
}

void setCXXLiteralOperatorNameLoc(SourceLocation Loc) {
CXXLiteralOperatorName.OpNameLoc = Loc;
}
Expand All @@ -739,12 +738,16 @@ class DeclarationNameLoc {

/// Return the beginning location of the getCXXOperatorNameRange() range.
SourceLocation getCXXOperatorNameBeginLoc() const {
return CXXOperatorName.BeginOpNameLoc;
if (!CXXOperatorName.OInfo)
return {};
return CXXOperatorName.OInfo->BeginOpNameLoc;
}

/// Return the end location of the getCXXOperatorNameRange() range.
SourceLocation getCXXOperatorNameEndLoc() const {
return CXXOperatorName.EndOpNameLoc;
if (!CXXOperatorName.OInfo)
return {};
return CXXOperatorName.OInfo->EndOpNameLoc;
}

/// Return the range of the operator name (without the operator keyword).
Expand All @@ -771,15 +774,10 @@ class DeclarationNameLoc {
}

/// Construct location information for a non-literal C++ operator.
static DeclarationNameLoc makeCXXOperatorNameLoc(SourceLocation BeginLoc,
SourceLocation EndLoc) {
return makeCXXOperatorNameLoc(SourceRange(BeginLoc, EndLoc));
}

/// Construct location information for a non-literal C++ operator.
static DeclarationNameLoc makeCXXOperatorNameLoc(SourceRange Range) {
static DeclarationNameLoc
makeCXXOperatorNameLoc(CXXOperatorSourceInfo *OInfo) {
DeclarationNameLoc DNL;
DNL.setCXXOperatorNameRange(Range);
DNL.CXXOperatorName.OInfo = OInfo;
return DNL;
}

Expand Down Expand Up @@ -849,6 +847,13 @@ struct DeclarationNameInfo {
LocInfo = DeclarationNameLoc::makeNamedTypeLoc(TInfo);
}

/// Sets the range of the operator name (without the operator keyword).
/// Assumes it is a C++ operator.
void setCXXOperatorNameInfo(CXXOperatorSourceInfo *OInfo) {
assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
LocInfo = DeclarationNameLoc::makeCXXOperatorNameLoc(OInfo);
}

/// getCXXOperatorNameRange - Gets the range of the operator name
/// (without the operator keyword). Assumes it is a (non-literal) operator.
SourceRange getCXXOperatorNameRange() const {
Expand All @@ -857,13 +862,6 @@ struct DeclarationNameInfo {
return LocInfo.getCXXOperatorNameRange();
}

/// setCXXOperatorNameRange - Sets the range of the operator name
/// (without the operator keyword). Assumes it is a C++ operator.
void setCXXOperatorNameRange(SourceRange R) {
assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
LocInfo = DeclarationNameLoc::makeCXXOperatorNameLoc(R);
}

/// getCXXLiteralOperatorNameLoc - Returns the location of the literal
/// operator name (not the operator keyword).
/// Assumes it is a literal operator.
Expand Down
66 changes: 40 additions & 26 deletions clang/include/clang/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ class OpaqueValueExpr : public Expr {
ExprObjectKind OK = OK_Ordinary, Expr *SourceExpr = nullptr)
: Expr(OpaqueValueExprClass, T, VK, OK), SourceExpr(SourceExpr) {
setIsUnique(false);
OpaqueValueExprBits.Loc = Loc;
OpaqueValueExprBits.Loc = Loc.getRawEncoding();
setDependence(computeDependence(this));
}

Expand All @@ -1195,7 +1195,9 @@ class OpaqueValueExpr : public Expr {
: Expr(OpaqueValueExprClass, Empty) {}

/// Retrieve the location of this expression.
SourceLocation getLocation() const { return OpaqueValueExprBits.Loc; }
SourceLocation getLocation() const {
return SourceLocation::getFromRawEncoding(OpaqueValueExprBits.Loc);
}

SourceLocation getBeginLoc() const LLVM_READONLY {
return SourceExpr ? SourceExpr->getBeginLoc() : getLocation();
Expand Down Expand Up @@ -1270,6 +1272,9 @@ class DeclRefExpr final
friend class ASTStmtWriter;
friend TrailingObjects;

/// The location of the declaration name itself.
SourceLocation Loc;

/// The declaration that we are referencing.
ValueDecl *D;

Expand Down Expand Up @@ -1341,13 +1346,13 @@ class DeclRefExpr final
return DeclarationNameInfo(getDecl()->getDeclName(), getLocation(), DNLoc);
}

SourceLocation getLocation() const { return DeclRefExprBits.Loc; }
void setLocation(SourceLocation L) { DeclRefExprBits.Loc = L; }
SourceLocation getLocation() const { return Loc; }
void setLocation(SourceLocation L) { Loc = L; }

SourceLocation getBeginLoc() const {
if (hasQualifier())
return getQualifierLoc().getBeginLoc();
return DeclRefExprBits.Loc;
return Loc;
}

SourceLocation getEndLoc() const LLVM_READONLY;
Expand Down Expand Up @@ -2004,6 +2009,9 @@ class PredefinedExpr final
friend class ASTStmtReader;
friend TrailingObjects;

/// The location of this PredefinedExpr.
SourceLocation Loc;

// PredefinedExpr is optionally followed by a single trailing
// "Stmt *" for the predefined identifier. It is present if and only if
// hasFunctionName() is true and is always a "StringLiteral *".
Expand Down Expand Up @@ -2041,8 +2049,8 @@ class PredefinedExpr final

bool isTransparent() const { return PredefinedExprBits.IsTransparent; }

SourceLocation getLocation() const { return PredefinedExprBits.Loc; }
void setLocation(SourceLocation L) { PredefinedExprBits.Loc = L; }
SourceLocation getLocation() const { return Loc; }
void setLocation(SourceLocation L) { Loc = L; }

StringLiteral *getFunctionName() {
return hasFunctionName()
Expand Down Expand Up @@ -2240,6 +2248,7 @@ class ParenExpr : public Expr {
class UnaryOperator final
: public Expr,
private llvm::TrailingObjects<UnaryOperator, FPOptionsOverride> {
SourceLocation Loc;
Stmt *Val;

FPOptionsOverride &getTrailingFPFeatures() {
Expand Down Expand Up @@ -2284,8 +2293,8 @@ class UnaryOperator final
void setSubExpr(Expr *E) { Val = E; }

/// getOperatorLoc - Return the location of the operator.
SourceLocation getOperatorLoc() const { return UnaryOperatorBits.Loc; }
void setOperatorLoc(SourceLocation L) { UnaryOperatorBits.Loc = L; }
SourceLocation getOperatorLoc() const { return Loc; }
void setOperatorLoc(SourceLocation L) { Loc = L; }

/// Returns true if the unary operator can cause an overflow. For instance,
/// signed int i = INT_MAX; i++;
Expand Down Expand Up @@ -2728,7 +2737,7 @@ class ArraySubscriptExpr : public Expr {
: Expr(ArraySubscriptExprClass, t, VK, OK) {
SubExprs[LHS] = lhs;
SubExprs[RHS] = rhs;
ArrayOrMatrixSubscriptExprBits.RBracketLoc = rbracketloc;
ArrayOrMatrixSubscriptExprBits.RBracketLoc = rbracketloc.getRawEncoding();
setDependence(computeDependence(this));
}

Expand Down Expand Up @@ -2765,10 +2774,11 @@ class ArraySubscriptExpr : public Expr {
SourceLocation getEndLoc() const { return getRBracketLoc(); }

SourceLocation getRBracketLoc() const {
return ArrayOrMatrixSubscriptExprBits.RBracketLoc;
return SourceLocation::getFromRawEncoding(
ArrayOrMatrixSubscriptExprBits.RBracketLoc);
}
void setRBracketLoc(SourceLocation L) {
ArrayOrMatrixSubscriptExprBits.RBracketLoc = L;
ArrayOrMatrixSubscriptExprBits.RBracketLoc = L.getRawEncoding();
}

SourceLocation getExprLoc() const LLVM_READONLY {
Expand Down Expand Up @@ -2806,7 +2816,7 @@ class MatrixSubscriptExpr : public Expr {
SubExprs[BASE] = Base;
SubExprs[ROW_IDX] = RowIdx;
SubExprs[COLUMN_IDX] = ColumnIdx;
ArrayOrMatrixSubscriptExprBits.RBracketLoc = RBracketLoc;
ArrayOrMatrixSubscriptExprBits.RBracketLoc = RBracketLoc.getRawEncoding();
setDependence(computeDependence(this));
}

Expand Down Expand Up @@ -2847,10 +2857,11 @@ class MatrixSubscriptExpr : public Expr {
}

SourceLocation getRBracketLoc() const {
return ArrayOrMatrixSubscriptExprBits.RBracketLoc;
return SourceLocation::getFromRawEncoding(
ArrayOrMatrixSubscriptExprBits.RBracketLoc);
}
void setRBracketLoc(SourceLocation L) {
ArrayOrMatrixSubscriptExprBits.RBracketLoc = L;
ArrayOrMatrixSubscriptExprBits.RBracketLoc = L.getRawEncoding();
}

static bool classof(const Stmt *T) {
Expand All @@ -2875,9 +2886,6 @@ class MatrixSubscriptExpr : public Expr {
class CallExpr : public Expr {
enum { FN = 0, PREARGS_START = 1 };

/// The number of arguments in the call expression.
unsigned NumArgs;

/// The location of the right parentheses. This has a different meaning for
/// the derived classes of CallExpr.
SourceLocation RParenLoc;
Expand All @@ -2904,7 +2912,7 @@ class CallExpr : public Expr {
// the begin source location, which has a significant impact on perf as
// getBeginLoc is assumed to be cheap.
// The layourt is as follow:
// CallExpr | Begin | 4 bytes left | Trailing Objects
// CallExpr | Begin | Trailing Objects
// CXXMemberCallExpr | Trailing Objects
// A bit in CallExprBitfields indicates if source locations are present.

Expand Down Expand Up @@ -3063,7 +3071,7 @@ class CallExpr : public Expr {
}

/// getNumArgs - Return the number of actual arguments to this call.
unsigned getNumArgs() const { return NumArgs; }
unsigned getNumArgs() const { return CallExprBits.NumArgs; }

/// Retrieve the call arguments.
Expr **getArgs() {
Expand Down Expand Up @@ -3111,13 +3119,15 @@ class CallExpr : public Expr {
void shrinkNumArgs(unsigned NewNumArgs) {
assert((NewNumArgs <= getNumArgs()) &&
"shrinkNumArgs cannot increase the number of arguments!");
NumArgs = NewNumArgs;
CallExprBits.NumArgs = NewNumArgs;
}

/// Bluntly set a new number of arguments without doing any checks whatsoever.
/// Only used during construction of a CallExpr in a few places in Sema.
/// FIXME: Find a way to remove it.
void setNumArgsUnsafe(unsigned NewNumArgs) { NumArgs = NewNumArgs; }
void setNumArgsUnsafe(unsigned NewNumArgs) {
CallExprBits.NumArgs = NewNumArgs;
}

typedef ExprIterator arg_iterator;
typedef ConstExprIterator const_arg_iterator;
Expand Down Expand Up @@ -3303,6 +3313,8 @@ class MemberExpr final
/// MemberLoc - This is the location of the member name.
SourceLocation MemberLoc;

SourceLocation OperatorLoc;

size_t numTrailingObjects(OverloadToken<NestedNameSpecifierLoc>) const {
return hasQualifier();
}
Expand Down Expand Up @@ -3464,7 +3476,7 @@ class MemberExpr final
MemberLoc, MemberDNLoc);
}

SourceLocation getOperatorLoc() const { return MemberExprBits.OperatorLoc; }
SourceLocation getOperatorLoc() const { return OperatorLoc; }

bool isArrow() const { return MemberExprBits.IsArrow; }
void setArrow(bool A) { MemberExprBits.IsArrow = A; }
Expand Down Expand Up @@ -3958,6 +3970,7 @@ class CStyleCastExpr final
class BinaryOperator : public Expr {
enum { LHS, RHS, END_EXPR };
Stmt *SubExprs[END_EXPR];
SourceLocation OpLoc;

public:
typedef BinaryOperatorKind Opcode;
Expand Down Expand Up @@ -3997,8 +4010,8 @@ class BinaryOperator : public Expr {
ExprObjectKind OK, SourceLocation opLoc,
FPOptionsOverride FPFeatures);
SourceLocation getExprLoc() const { return getOperatorLoc(); }
SourceLocation getOperatorLoc() const { return BinaryOperatorBits.OpLoc; }
void setOperatorLoc(SourceLocation L) { BinaryOperatorBits.OpLoc = L; }
SourceLocation getOperatorLoc() const { return OpLoc; }
void setOperatorLoc(SourceLocation L) { OpLoc = L; }

Opcode getOpcode() const {
return static_cast<Opcode>(BinaryOperatorBits.Opc);
Expand Down Expand Up @@ -6449,7 +6462,8 @@ class GenericSelectionExpr final
}

SourceLocation getGenericLoc() const {
return GenericSelectionExprBits.GenericLoc;
return SourceLocation::getFromRawEncoding(
GenericSelectionExprBits.GenericLoc);
}
SourceLocation getDefaultLoc() const { return DefaultLoc; }
SourceLocation getRParenLoc() const { return RParenLoc; }
Expand Down
Loading