diff --git a/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp b/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp index e1fb42b8210e2..ecde1f7c90080 100644 --- a/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp @@ -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 Details; // 25 elements is 200 bytes. + SmallVector 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. diff --git a/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp b/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp index 801d535c1b9d0..931241845c54a 100644 --- a/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp +++ b/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp @@ -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 { diff --git a/clang-tools-extra/include-cleaner/lib/HTMLReport.cpp b/clang-tools-extra/include-cleaner/lib/HTMLReport.cpp index b0a6c9565befd..1cc2db7031819 100644 --- a/clang-tools-extra/include-cleaner/lib/HTMLReport.cpp +++ b/clang-tools-extra/include-cleaner/lib/HTMLReport.cpp @@ -145,7 +145,7 @@ class Reporter { // Points within the main file that reference a Symbol. // Implicit refs will be marked with a symbol just before the token. struct Ref { - unsigned Offset; + uint64_t Offset; RefType Type; Symbol Sym; SmallVector Locations = {}; diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index 2b9cd035623cc..8ae212cc6cc94 100644 --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -3356,6 +3356,8 @@ class ASTContext : public RefCountedBase { 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. /// diff --git a/clang/include/clang/AST/DeclBase.h b/clang/include/clang/AST/DeclBase.h index dd67ebc9873ff..bdc92760ba092 100644 --- a/clang/include/clang/AST/DeclBase.h +++ b/clang/include/clang/AST/DeclBase.h @@ -1953,16 +1953,10 @@ class DeclContext { /// For the bits in DeclContextBitfields LLVM_PREFERRED_TYPE(DeclContextBitfields) uint32_t : NumDeclContextBits; - - // Not a bitfield but this saves space. - // Note that ObjCContainerDeclBitfields is full. - SourceLocation AtStart; }; /// 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 }; /// Stores the bits used by LinkageSpecDecl. /// If modified NumLinkageSpecDeclBits and the accessor diff --git a/clang/include/clang/AST/DeclObjC.h b/clang/include/clang/AST/DeclObjC.h index 9014d76f8433b..56fc4ba4177da 100644 --- a/clang/include/clang/AST/DeclObjC.h +++ b/clang/include/clang/AST/DeclObjC.h @@ -946,6 +946,7 @@ class ObjCContainerDecl : public NamedDecl, public DeclContext { // This class stores some data in DeclContext::ObjCContainerDeclBits // to save some space. Use the provided accessors to access it. + SourceLocation AtStart; // These two locations in the range mark the end of the method container. // The first points to the '@' token, and the second to the 'end' token. SourceRange AtEnd; @@ -1090,10 +1091,10 @@ 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 AtStart; } void setAtStartLoc(SourceLocation Loc) { - ObjCContainerDeclBits.AtStart = Loc; + AtStart = Loc; } // Marks the end of the container. diff --git a/clang/include/clang/AST/DeclarationName.h b/clang/include/clang/AST/DeclarationName.h index 284228dc0ee47..db7973bcc5bb7 100644 --- a/clang/include/clang/AST/DeclarationName.h +++ b/clang/include/clang/AST/DeclarationName.h @@ -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. @@ -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. @@ -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; } @@ -739,12 +738,17 @@ 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). @@ -769,17 +773,12 @@ class DeclarationNameLoc { DNL.setNamedTypeLoc(TInfo); return DNL; } - - /// 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; } @@ -839,7 +838,7 @@ struct DeclarationNameInfo { return nullptr; return LocInfo.getNamedTypeInfo(); } - + /// setNamedTypeInfo - Sets the source type info associated to /// the name. Assumes it is a constructor, destructor or conversion. void setNamedTypeInfo(TypeSourceInfo *TInfo) { @@ -849,6 +848,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 { @@ -857,13 +863,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. diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h index d95396fd59b95..6d1c2df6bdc88 100644 --- a/clang/include/clang/AST/Expr.h +++ b/clang/include/clang/AST/Expr.h @@ -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)); } @@ -1195,7 +1195,7 @@ 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(); @@ -1269,6 +1269,9 @@ class DeclRefExpr final friend class ASTStmtReader; friend class ASTStmtWriter; friend TrailingObjects; + + /// The location of the declaration name itself. + SourceLocation Loc; /// The declaration that we are referencing. ValueDecl *D; @@ -1341,13 +1344,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; @@ -2003,7 +2006,8 @@ class PredefinedExpr final private llvm::TrailingObjects { 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 *". @@ -2041,8 +2045,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() @@ -2240,6 +2244,7 @@ class ParenExpr : public Expr { class UnaryOperator final : public Expr, private llvm::TrailingObjects { + SourceLocation Loc; Stmt *Val; FPOptionsOverride &getTrailingFPFeatures() { @@ -2284,8 +2289,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++; @@ -2728,7 +2733,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)); } @@ -2765,10 +2770,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 { @@ -2806,7 +2812,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)); } @@ -2847,10 +2853,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) { @@ -2875,9 +2882,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; @@ -2897,19 +2901,19 @@ class CallExpr : public Expr { // // * An optional of type FPOptionsOverride. // - // CallExpr subclasses are asssumed to be 32 bytes or less, and CallExpr + // CallExpr subclasses are asssumed to be 40 bytes or less, and CallExpr // itself is 24 bytes. To avoid having to recompute or store the offset of the - // trailing objects, we put it at 32 bytes (such that it is suitable for all + // trailing objects, we put it at 40 bytes (such that it is suitable for all // subclasses) We use the 8 bytes gap left for instances of CallExpr to store // 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 | 8 bytes left | Trailing Objects // CXXMemberCallExpr | Trailing Objects // A bit in CallExprBitfields indicates if source locations are present. protected: - static constexpr unsigned OffsetToTrailingObjects = 32; + static constexpr unsigned OffsetToTrailingObjects = 40; template static constexpr unsigned sizeToAllocateForCallExprSubclass(unsigned SizeOfTrailingObjects) { @@ -3063,7 +3067,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() { @@ -3111,13 +3115,13 @@ 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; @@ -3302,6 +3306,8 @@ class MemberExpr final /// MemberLoc - This is the location of the member name. SourceLocation MemberLoc; + + SourceLocation OperatorLoc; size_t numTrailingObjects(OverloadToken) const { return hasQualifier(); @@ -3464,7 +3470,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; } @@ -3958,6 +3964,7 @@ class CStyleCastExpr final class BinaryOperator : public Expr { enum { LHS, RHS, END_EXPR }; Stmt *SubExprs[END_EXPR]; + SourceLocation OpLoc; public: typedef BinaryOperatorKind Opcode; @@ -3997,8 +4004,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(BinaryOperatorBits.Opc); @@ -6099,7 +6106,6 @@ class GenericSelectionExpr final friend class ASTStmtReader; friend class ASTStmtWriter; friend TrailingObjects; - /// The number of association expressions and the index of the result /// expression in the case where the generic selection expression is not /// result-dependent. The result index is equal to ResultDependentIndex @@ -6449,7 +6455,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; } diff --git a/clang/include/clang/AST/ExprCXX.h b/clang/include/clang/AST/ExprCXX.h index 477373f07f25d..98f40b8a89bf6 100644 --- a/clang/include/clang/AST/ExprCXX.h +++ b/clang/include/clang/AST/ExprCXX.h @@ -724,7 +724,7 @@ class CXXBoolLiteralExpr : public Expr { CXXBoolLiteralExpr(bool Val, QualType Ty, SourceLocation Loc) : Expr(CXXBoolLiteralExprClass, Ty, VK_PRValue, OK_Ordinary) { CXXBoolLiteralExprBits.Value = Val; - CXXBoolLiteralExprBits.Loc = Loc; + CXXBoolLiteralExprBits.Loc = Loc.getRawEncoding(); setDependence(ExprDependence::None); } @@ -742,8 +742,12 @@ class CXXBoolLiteralExpr : public Expr { SourceLocation getBeginLoc() const { return getLocation(); } SourceLocation getEndLoc() const { return getLocation(); } - SourceLocation getLocation() const { return CXXBoolLiteralExprBits.Loc; } - void setLocation(SourceLocation L) { CXXBoolLiteralExprBits.Loc = L; } + SourceLocation getLocation() const { + return SourceLocation::getFromRawEncoding(CXXBoolLiteralExprBits.Loc); + } + void setLocation(SourceLocation L) { + CXXBoolLiteralExprBits.Loc = L.getRawEncoding(); + } static bool classof(const Stmt *T) { return T->getStmtClass() == CXXBoolLiteralExprClass; @@ -768,7 +772,7 @@ class CXXNullPtrLiteralExpr : public Expr { public: CXXNullPtrLiteralExpr(QualType Ty, SourceLocation Loc) : Expr(CXXNullPtrLiteralExprClass, Ty, VK_PRValue, OK_Ordinary) { - CXXNullPtrLiteralExprBits.Loc = Loc; + CXXNullPtrLiteralExprBits.Loc = Loc.getRawEncoding(); setDependence(ExprDependence::None); } @@ -778,8 +782,12 @@ class CXXNullPtrLiteralExpr : public Expr { SourceLocation getBeginLoc() const { return getLocation(); } SourceLocation getEndLoc() const { return getLocation(); } - SourceLocation getLocation() const { return CXXNullPtrLiteralExprBits.Loc; } - void setLocation(SourceLocation L) { CXXNullPtrLiteralExprBits.Loc = L; } + SourceLocation getLocation() const { + return SourceLocation::getFromRawEncoding(CXXNullPtrLiteralExprBits.Loc); + } + void setLocation(SourceLocation L) { + CXXNullPtrLiteralExprBits.Loc = L.getRawEncoding(); + } static bool classof(const Stmt *T) { return T->getStmtClass() == CXXNullPtrLiteralExprClass; @@ -1152,11 +1160,12 @@ class CXXUuidofExpr : public Expr { /// }; /// \endcode class CXXThisExpr : public Expr { + CXXThisExpr(SourceLocation L, QualType Ty, bool IsImplicit, ExprValueKind VK) : Expr(CXXThisExprClass, Ty, VK, OK_Ordinary) { CXXThisExprBits.IsImplicit = IsImplicit; CXXThisExprBits.CapturedByCopyInLambdaWithExplicitObjectParameter = false; - CXXThisExprBits.Loc = L; + CXXThisExprBits.Loc = L.getRawEncoding(); setDependence(computeDependence(this)); } @@ -1168,8 +1177,12 @@ class CXXThisExpr : public Expr { static CXXThisExpr *CreateEmpty(const ASTContext &Ctx); - SourceLocation getLocation() const { return CXXThisExprBits.Loc; } - void setLocation(SourceLocation L) { CXXThisExprBits.Loc = L; } + SourceLocation getLocation() const { + return SourceLocation::getFromRawEncoding(CXXThisExprBits.Loc); + } + void setLocation(SourceLocation L) { + CXXThisExprBits.Loc = L.getRawEncoding(); + } SourceLocation getBeginLoc() const { return getLocation(); } SourceLocation getEndLoc() const { return getLocation(); } @@ -1219,7 +1232,7 @@ class CXXThrowExpr : public Expr { CXXThrowExpr(Expr *Operand, QualType Ty, SourceLocation Loc, bool IsThrownVariableInScope) : Expr(CXXThrowExprClass, Ty, VK_PRValue, OK_Ordinary), Operand(Operand) { - CXXThrowExprBits.ThrowLoc = Loc; + CXXThrowExprBits.ThrowLoc = Loc.getRawEncoding(); CXXThrowExprBits.IsThrownVariableInScope = IsThrownVariableInScope; setDependence(computeDependence(this)); } @@ -1228,7 +1241,9 @@ class CXXThrowExpr : public Expr { const Expr *getSubExpr() const { return cast_or_null(Operand); } Expr *getSubExpr() { return cast_or_null(Operand); } - SourceLocation getThrowLoc() const { return CXXThrowExprBits.ThrowLoc; } + SourceLocation getThrowLoc() const { + return SourceLocation::getFromRawEncoding(CXXThrowExprBits.ThrowLoc); + } /// Determines whether the variable thrown by this expression (if any!) /// is within the innermost try block. @@ -1287,7 +1302,7 @@ class CXXDefaultArgExpr final Param->getDefaultArg()->getValueKind(), Param->getDefaultArg()->getObjectKind()), Param(Param), UsedContext(UsedContext) { - CXXDefaultArgExprBits.Loc = Loc; + CXXDefaultArgExprBits.Loc = Loc.getRawEncoding(); CXXDefaultArgExprBits.HasRewrittenInit = RewrittenExpr != nullptr; if (RewrittenExpr) *getTrailingObjects() = RewrittenExpr; @@ -1341,7 +1356,9 @@ class CXXDefaultArgExpr final DeclContext *getUsedContext() { return UsedContext; } /// Retrieve the location where this default argument was actually used. - SourceLocation getUsedLocation() const { return CXXDefaultArgExprBits.Loc; } + SourceLocation getUsedLocation() const { + return SourceLocation::getFromRawEncoding(CXXDefaultArgExprBits.Loc); + } /// Default argument expressions have no representation in the /// source, so they have an empty source range. @@ -1384,7 +1401,6 @@ class CXXDefaultInitExpr final /// The context where the default initializer expression was used. DeclContext *UsedContext; - CXXDefaultInitExpr(const ASTContext &Ctx, SourceLocation Loc, FieldDecl *Field, QualType Ty, DeclContext *UsedContext, Expr *RewrittenInitExpr); @@ -1438,8 +1454,10 @@ class CXXDefaultInitExpr final /// actually used. SourceLocation getUsedLocation() const { return getBeginLoc(); } - SourceLocation getBeginLoc() const { return CXXDefaultInitExprBits.Loc; } - SourceLocation getEndLoc() const { return CXXDefaultInitExprBits.Loc; } + SourceLocation getBeginLoc() const { + return SourceLocation::getFromRawEncoding(CXXDefaultInitExprBits.Loc); + } + SourceLocation getEndLoc() const { return getBeginLoc(); } static bool classof(const Stmt *T) { return T->getStmtClass() == CXXDefaultInitExprClass; @@ -1555,7 +1573,7 @@ class CXXConstructExpr : public Expr { /// The number of arguments. unsigned NumArgs; - + SourceLocation Loc; // We would like to stash the arguments of the constructor call after // CXXConstructExpr. However CXXConstructExpr is used as a base class of // CXXTemporaryObjectExpr which makes the use of llvm::TrailingObjects @@ -1610,8 +1628,8 @@ class CXXConstructExpr : public Expr { /// Get the constructor that this expression will (ultimately) call. CXXConstructorDecl *getConstructor() const { return Constructor; } - SourceLocation getLocation() const { return CXXConstructExprBits.Loc; } - void setLocation(SourceLocation Loc) { CXXConstructExprBits.Loc = Loc; } + SourceLocation getLocation() const { return Loc; } + void setLocation(SourceLocation Loc) { this->Loc = Loc; } /// Whether this construction is elidable. bool isElidable() const { return CXXConstructExprBits.Elidable; } @@ -2193,7 +2211,7 @@ class CXXScalarValueInitExpr : public Expr { SourceLocation RParenLoc) : Expr(CXXScalarValueInitExprClass, Type, VK_PRValue, OK_Ordinary), TypeInfo(TypeInfo) { - CXXScalarValueInitExprBits.RParenLoc = RParenLoc; + CXXScalarValueInitExprBits.RParenLoc = RParenLoc.getRawEncoding(); setDependence(computeDependence(this)); } @@ -2205,7 +2223,8 @@ class CXXScalarValueInitExpr : public Expr { } SourceLocation getRParenLoc() const { - return CXXScalarValueInitExprBits.RParenLoc; + return SourceLocation::getFromRawEncoding( + CXXScalarValueInitExprBits.RParenLoc); } SourceLocation getBeginLoc() const LLVM_READONLY; @@ -2622,7 +2641,7 @@ class CXXDeleteExpr : public Expr { CXXDeleteExprBits.ArrayForm = ArrayForm; CXXDeleteExprBits.ArrayFormAsWritten = ArrayFormAsWritten; CXXDeleteExprBits.UsualArrayDeleteWantsSize = UsualArrayDeleteWantsSize; - CXXDeleteExprBits.Loc = Loc; + CXXDeleteExprBits.Loc = Loc.getRawEncoding(); setDependence(computeDependence(this)); } @@ -2653,7 +2672,9 @@ class CXXDeleteExpr : public Expr { /// be a pointer, return an invalid type. QualType getDestroyedType() const; - SourceLocation getBeginLoc() const { return CXXDeleteExprBits.Loc; } + SourceLocation getBeginLoc() const { + return SourceLocation::getFromRawEncoding(CXXDeleteExprBits.Loc); + } SourceLocation getEndLoc() const LLVM_READONLY { return Argument->getEndLoc(); } @@ -3908,7 +3929,7 @@ class CXXDependentScopeMemberExpr final /// Retrieve the location of the '->' or '.' operator. SourceLocation getOperatorLoc() const { - return CXXDependentScopeMemberExprBits.OperatorLoc; + return SourceLocation::getFromRawEncoding(CXXDependentScopeMemberExprBits.OperatorLoc); } /// Retrieve the nested-name-specifier that qualifies the member name. @@ -4630,12 +4651,12 @@ class SubstNonTypeTemplateParmExpr : public Expr { AssociatedDeclAndRef(AssociatedDecl, RefParam), Index(Index), PackIndex(PackIndex.toInternalRepresentation()), Final(Final) { assert(AssociatedDecl != nullptr); - SubstNonTypeTemplateParmExprBits.NameLoc = Loc; + SubstNonTypeTemplateParmExprBits.NameLoc = Loc.getRawEncoding(); setDependence(computeDependence(this)); } SourceLocation getNameLoc() const { - return SubstNonTypeTemplateParmExprBits.NameLoc; + return SourceLocation::getFromRawEncoding(SubstNonTypeTemplateParmExprBits.NameLoc); } SourceLocation getBeginLoc() const { return getNameLoc(); } SourceLocation getEndLoc() const { return getNameLoc(); } diff --git a/clang/include/clang/AST/ExprConcepts.h b/clang/include/clang/AST/ExprConcepts.h index 8df5cdcaa9d75..c6724315194f9 100644 --- a/clang/include/clang/AST/ExprConcepts.h +++ b/clang/include/clang/AST/ExprConcepts.h @@ -502,6 +502,7 @@ class RequiresExpr final : public Expr, concepts::Requirement *> { friend TrailingObjects; friend class ASTStmtReader; + friend class ASTStmtWriter; unsigned NumLocalParameters; unsigned NumRequirements; @@ -560,7 +561,7 @@ class RequiresExpr final : public Expr, } SourceLocation getRequiresKWLoc() const { - return RequiresExprBits.RequiresKWLoc; + return SourceLocation::getFromRawEncoding(RequiresExprBits.RequiresKWLoc); } SourceLocation getLParenLoc() const { return LParenLoc; } @@ -572,7 +573,7 @@ class RequiresExpr final : public Expr, } SourceLocation getBeginLoc() const LLVM_READONLY { - return RequiresExprBits.RequiresKWLoc; + return getRequiresKWLoc(); } SourceLocation getEndLoc() const LLVM_READONLY { return RBraceLoc; diff --git a/clang/include/clang/AST/ExternalASTSource.h b/clang/include/clang/AST/ExternalASTSource.h index e91d5132da10f..01dd56b576200 100644 --- a/clang/include/clang/AST/ExternalASTSource.h +++ b/clang/include/clang/AST/ExternalASTSource.h @@ -110,7 +110,7 @@ class ExternalASTSource : public RefCountedBase { /// returns non-zero for GetNumKnownSelectors(). /// /// The default implementation of this method is a no-op. - virtual Selector GetExternalSelector(uint32_t ID); + virtual Selector GetExternalSelector(uint64_t ID); /// Returns the number of selectors known to the external AST /// source. diff --git a/clang/include/clang/AST/Stmt.h b/clang/include/clang/AST/Stmt.h index a5b0d5053003f..9a363f25fc989 100644 --- a/clang/include/clang/AST/Stmt.h +++ b/clang/include/clang/AST/Stmt.h @@ -128,7 +128,7 @@ class alignas(void *) Stmt { friend class NullStmt; LLVM_PREFERRED_TYPE(StmtBitfields) - unsigned : NumStmtBits; + uint64_t : NumStmtBits; /// True if the null statement was preceded by an empty macro, e.g: /// @code @@ -136,10 +136,11 @@ class alignas(void *) Stmt { /// CALL(0); /// @endcode LLVM_PREFERRED_TYPE(bool) - unsigned HasLeadingEmptyMacro : 1; + uint64_t HasLeadingEmptyMacro : 1; - /// The location of the semi-colon. - SourceLocation SemiLoc; + /// The location of the semi-colon.Add commentMore actions + LLVM_PREFERRED_TYPE(SourceLocation) + uint64_t SemiLoc : SourceLocation::Bits; }; class CompoundStmtBitfields { @@ -153,7 +154,7 @@ class alignas(void *) Stmt { /// floating-point features. LLVM_PREFERRED_TYPE(bool) unsigned HasFPFeatures : 1; - + unsigned NumStmts; }; @@ -161,9 +162,10 @@ class alignas(void *) Stmt { friend class LabelStmt; LLVM_PREFERRED_TYPE(StmtBitfields) - unsigned : NumStmtBits; + uint64_t : NumStmtBits; - SourceLocation IdentLoc; + LLVM_PREFERRED_TYPE(SourceLocation) + uint64_t IdentLoc : SourceLocation::Bits; }; class AttributedStmtBitfields { @@ -175,9 +177,6 @@ class alignas(void *) Stmt { /// Number of attributes. unsigned NumAttrs : 32 - NumStmtBits; - - /// The location of the attribute. - SourceLocation AttrLoc; }; class IfStmtBitfields { @@ -185,50 +184,52 @@ class alignas(void *) Stmt { friend class IfStmt; LLVM_PREFERRED_TYPE(StmtBitfields) - unsigned : NumStmtBits; + uint64_t : NumStmtBits; /// Whether this is a constexpr if, or a consteval if, or neither. LLVM_PREFERRED_TYPE(IfStatementKind) - unsigned Kind : 3; + uint64_t Kind : 3; /// True if this if statement has storage for an else statement. LLVM_PREFERRED_TYPE(bool) - unsigned HasElse : 1; + uint64_t HasElse : 1; /// True if this if statement has storage for a variable declaration. LLVM_PREFERRED_TYPE(bool) - unsigned HasVar : 1; + uint64_t HasVar : 1; /// True if this if statement has storage for an init statement. LLVM_PREFERRED_TYPE(bool) - unsigned HasInit : 1; + uint64_t HasInit : 1; - /// The location of the "if". - SourceLocation IfLoc; + /// The location of the "if" + LLVM_PREFERRED_TYPE(SourceLocation) + uint64_t IfLoc : SourceLocation::Bits; }; class SwitchStmtBitfields { friend class SwitchStmt; LLVM_PREFERRED_TYPE(StmtBitfields) - unsigned : NumStmtBits; + uint64_t : NumStmtBits; /// True if the SwitchStmt has storage for an init statement. LLVM_PREFERRED_TYPE(bool) - unsigned HasInit : 1; + uint64_t HasInit : 1; /// True if the SwitchStmt has storage for a condition variable. LLVM_PREFERRED_TYPE(bool) - unsigned HasVar : 1; + uint64_t HasVar : 1; /// If the SwitchStmt is a switch on an enum value, records whether all /// the enum values were covered by CaseStmts. The coverage information /// value is meant to be a hint for possible clients. LLVM_PREFERRED_TYPE(bool) - unsigned AllEnumCasesCovered : 1; + uint64_t AllEnumCasesCovered : 1; /// The location of the "switch". - SourceLocation SwitchLoc; + LLVM_PREFERRED_TYPE(SourceLocation) + uint64_t SwitchLoc : SourceLocation::Bits; }; class WhileStmtBitfields { @@ -236,34 +237,37 @@ class alignas(void *) Stmt { friend class WhileStmt; LLVM_PREFERRED_TYPE(StmtBitfields) - unsigned : NumStmtBits; + uint64_t : NumStmtBits; /// True if the WhileStmt has storage for a condition variable. LLVM_PREFERRED_TYPE(bool) - unsigned HasVar : 1; + uint64_t HasVar : 1; - /// The location of the "while". - SourceLocation WhileLoc; + /// The location of the "while" + LLVM_PREFERRED_TYPE(SourceLocation) + uint64_t WhileLoc : SourceLocation::Bits; }; class DoStmtBitfields { friend class DoStmt; LLVM_PREFERRED_TYPE(StmtBitfields) - unsigned : NumStmtBits; + uint64_t : NumStmtBits; - /// The location of the "do". - SourceLocation DoLoc; + /// The location of the "do" + LLVM_PREFERRED_TYPE(SourceLocation) + uint64_t DoLoc : SourceLocation::Bits; }; class ForStmtBitfields { friend class ForStmt; LLVM_PREFERRED_TYPE(StmtBitfields) - unsigned : NumStmtBits; + uint64_t : NumStmtBits; - /// The location of the "for". - SourceLocation ForLoc; + /// The location of the "for" + LLVM_PREFERRED_TYPE(SourceLocation) + uint64_t ForLoc : SourceLocation::Bits; }; class GotoStmtBitfields { @@ -271,44 +275,48 @@ class alignas(void *) Stmt { friend class IndirectGotoStmt; LLVM_PREFERRED_TYPE(StmtBitfields) - unsigned : NumStmtBits; + uint64_t : NumStmtBits; - /// The location of the "goto". - SourceLocation GotoLoc; + /// The location of the "goto" + LLVM_PREFERRED_TYPE(SourceLocation) + uint64_t GotoLoc : SourceLocation::Bits; }; class ContinueStmtBitfields { friend class ContinueStmt; LLVM_PREFERRED_TYPE(StmtBitfields) - unsigned : NumStmtBits; + uint64_t : NumStmtBits; - /// The location of the "continue". - SourceLocation ContinueLoc; + /// The location of the "continue" + LLVM_PREFERRED_TYPE(SourceLocation) + uint64_t ContinueLoc : SourceLocation::Bits; }; class BreakStmtBitfields { friend class BreakStmt; LLVM_PREFERRED_TYPE(StmtBitfields) - unsigned : NumStmtBits; + uint64_t : NumStmtBits; - /// The location of the "break". - SourceLocation BreakLoc; + /// The location of the "break" + LLVM_PREFERRED_TYPE(SourceLocation) + uint64_t BreakLoc : SourceLocation::Bits; }; class ReturnStmtBitfields { friend class ReturnStmt; LLVM_PREFERRED_TYPE(StmtBitfields) - unsigned : NumStmtBits; + uint64_t : NumStmtBits; /// True if this ReturnStmt has storage for an NRVO candidate. LLVM_PREFERRED_TYPE(bool) - unsigned HasNRVOCandidate : 1; + uint64_t HasNRVOCandidate : 1; - /// The location of the "return". - SourceLocation RetLoc; + /// The location of the "return" + LLVM_PREFERRED_TYPE(SourceLocation) + uint64_t RetLoc : SourceLocation::Bits; }; class SwitchCaseBitfields { @@ -316,15 +324,16 @@ class alignas(void *) Stmt { friend class CaseStmt; LLVM_PREFERRED_TYPE(StmtBitfields) - unsigned : NumStmtBits; + uint64_t : NumStmtBits; /// Used by CaseStmt to store whether it is a case statement /// of the form case LHS ... RHS (a GNU extension). LLVM_PREFERRED_TYPE(bool) - unsigned CaseStmtIsGNURange : 1; + uint64_t CaseStmtIsGNURange : 1; /// The location of the "case" or "default" keyword. - SourceLocation KeywordLoc; + LLVM_PREFERRED_TYPE(SourceLocation) + uint64_t KeywordLoc : SourceLocation::Bits; }; //===--- Expression bitfields classes ---===// @@ -421,9 +430,6 @@ class alignas(void *) Stmt { /// MSVC compatibility). LLVM_PREFERRED_TYPE(bool) unsigned IsTransparent : 1; - - /// The location of this PredefinedExpr. - SourceLocation Loc; }; class DeclRefExprBitfields { @@ -450,8 +456,7 @@ class alignas(void *) Stmt { LLVM_PREFERRED_TYPE(bool) unsigned IsImmediateEscalating : 1; - /// The location of the declaration name itself. - SourceLocation Loc; + }; @@ -522,7 +527,7 @@ class alignas(void *) Stmt { LLVM_PREFERRED_TYPE(bool) unsigned HasFPFeatures : 1; - SourceLocation Loc; + }; class UnaryExprOrTypeTraitExprBitfields { @@ -542,9 +547,10 @@ class alignas(void *) Stmt { friend class MatrixSubscriptExpr; LLVM_PREFERRED_TYPE(ExprBitfields) - unsigned : NumExprBits; + uint64_t : NumExprBits; - SourceLocation RBracketLoc; + LLVM_PREFERRED_TYPE(SourceLocation) + uint64_t RBracketLoc : SourceLocation::Bits; }; class CallExprBitfields { @@ -576,9 +582,10 @@ class alignas(void *) Stmt { /// Trailing objects. See the definition of CallExpr. LLVM_PREFERRED_TYPE(bool) unsigned HasTrailingSourceLoc : 1; + + unsigned NumArgs:20; }; - - enum { NumCallExprBits = 25 }; + enum { NumCallExprBits = 52 }; class MemberExprBitfields { friend class ASTStmtReader; @@ -620,9 +627,10 @@ class alignas(void *) Stmt { unsigned NonOdrUseReason : 2; /// This is the location of the -> or . in the expression. - SourceLocation OperatorLoc; + // SourceLocation OperatorLoc; }; - + + // 8 bytes class CastExprBitfields { friend class CastExpr; friend class ImplicitCastExpr; @@ -663,8 +671,6 @@ class alignas(void *) Stmt { /// overflow sanitization. LLVM_PREFERRED_TYPE(bool) unsigned ExcludedOverflowPattern : 1; - - SourceLocation OpLoc; }; class InitListExprBitfields { @@ -695,10 +701,11 @@ class alignas(void *) Stmt { friend class GenericSelectionExpr; LLVM_PREFERRED_TYPE(ExprBitfields) - unsigned : NumExprBits; + uint64_t : NumExprBits; - /// The location of the "_Generic". - SourceLocation GenericLoc; + /// The location of the "_Generic" + LLVM_PREFERRED_TYPE(SourceLocation) + uint64_t GenericLoc : SourceLocation::Bits; }; class PseudoObjectExprBitfields { @@ -777,12 +784,12 @@ class alignas(void *) Stmt { friend class CXXOperatorCallExpr; LLVM_PREFERRED_TYPE(CallExprBitfields) - unsigned : NumCallExprBits; + uint64_t : NumCallExprBits; /// The kind of this overloaded operator. One of the enumerator /// value of OverloadedOperatorKind. LLVM_PREFERRED_TYPE(OverloadedOperatorKind) - unsigned OperatorKind : 6; + uint64_t OperatorKind : 6; }; class CXXRewrittenBinaryOperatorBitfields { @@ -790,53 +797,56 @@ class alignas(void *) Stmt { friend class CXXRewrittenBinaryOperator; LLVM_PREFERRED_TYPE(CallExprBitfields) - unsigned : NumCallExprBits; + uint64_t : NumCallExprBits; LLVM_PREFERRED_TYPE(bool) - unsigned IsReversed : 1; + uint64_t IsReversed : 1; }; class CXXBoolLiteralExprBitfields { friend class CXXBoolLiteralExpr; LLVM_PREFERRED_TYPE(ExprBitfields) - unsigned : NumExprBits; + uint64_t : NumExprBits; /// The value of the boolean literal. LLVM_PREFERRED_TYPE(bool) - unsigned Value : 1; + uint64_t Value : 1; - /// The location of the boolean literal. - SourceLocation Loc; + /// The location of the boolean ligeral + LLVM_PREFERRED_TYPE(SourceLocation) + uint64_t Loc : SourceLocation::Bits; }; class CXXNullPtrLiteralExprBitfields { friend class CXXNullPtrLiteralExpr; LLVM_PREFERRED_TYPE(ExprBitfields) - unsigned : NumExprBits; + uint64_t : NumExprBits; /// The location of the null pointer literal. - SourceLocation Loc; + LLVM_PREFERRED_TYPE(SourceLocation) + uint64_t Loc : SourceLocation::Bits; }; class CXXThisExprBitfields { friend class CXXThisExpr; LLVM_PREFERRED_TYPE(ExprBitfields) - unsigned : NumExprBits; + uint64_t : NumExprBits; /// Whether this is an implicit "this". LLVM_PREFERRED_TYPE(bool) - unsigned IsImplicit : 1; + uint64_t IsImplicit : 1; /// Whether there is a lambda with an explicit object parameter that /// captures this "this" by copy. LLVM_PREFERRED_TYPE(bool) - unsigned CapturedByCopyInLambdaWithExplicitObjectParameter : 1; + uint64_t CapturedByCopyInLambdaWithExplicitObjectParameter : 1; - /// The location of the "this". - SourceLocation Loc; + /// The location of the "this" + LLVM_PREFERRED_TYPE(SourceLocation) + uint64_t Loc : SourceLocation::Bits; }; class CXXThrowExprBitfields { @@ -844,14 +854,15 @@ class alignas(void *) Stmt { friend class CXXThrowExpr; LLVM_PREFERRED_TYPE(ExprBitfields) - unsigned : NumExprBits; + uint64_t : NumExprBits; /// Whether the thrown variable (if any) is in scope. LLVM_PREFERRED_TYPE(bool) - unsigned IsThrownVariableInScope : 1; + uint64_t IsThrownVariableInScope : 1; - /// The location of the "throw". - SourceLocation ThrowLoc; + /// The location of the "throw" + LLVM_PREFERRED_TYPE(SourceLocation) + uint64_t ThrowLoc : SourceLocation::Bits; }; class CXXDefaultArgExprBitfields { @@ -859,14 +870,15 @@ class alignas(void *) Stmt { friend class CXXDefaultArgExpr; LLVM_PREFERRED_TYPE(ExprBitfields) - unsigned : NumExprBits; + uint64_t : NumExprBits; /// Whether this CXXDefaultArgExpr rewrote its argument and stores a copy. LLVM_PREFERRED_TYPE(bool) - unsigned HasRewrittenInit : 1; + uint64_t HasRewrittenInit : 1; /// The location where the default argument expression was used. - SourceLocation Loc; + LLVM_PREFERRED_TYPE(SourceLocation) + uint64_t Loc : SourceLocation::Bits; }; class CXXDefaultInitExprBitfields { @@ -874,15 +886,16 @@ class alignas(void *) Stmt { friend class CXXDefaultInitExpr; LLVM_PREFERRED_TYPE(ExprBitfields) - unsigned : NumExprBits; + uint64_t : NumExprBits; /// Whether this CXXDefaultInitExprBitfields rewrote its argument and stores /// a copy. LLVM_PREFERRED_TYPE(bool) - unsigned HasRewrittenInit : 1; + uint64_t HasRewrittenInit : 1; /// The location where the default initializer expression was used. - SourceLocation Loc; + LLVM_PREFERRED_TYPE(SourceLocation) + uint64_t Loc : SourceLocation::Bits; }; class CXXScalarValueInitExprBitfields { @@ -890,9 +903,11 @@ class alignas(void *) Stmt { friend class CXXScalarValueInitExpr; LLVM_PREFERRED_TYPE(ExprBitfields) - unsigned : NumExprBits; + uint64_t : NumExprBits; - SourceLocation RParenLoc; + /// The location where the default initializer expression was used. + LLVM_PREFERRED_TYPE(SourceLocation) + uint64_t RParenLoc : SourceLocation::Bits; }; class CXXNewExprBitfields { @@ -946,29 +961,30 @@ class alignas(void *) Stmt { friend class CXXDeleteExpr; LLVM_PREFERRED_TYPE(ExprBitfields) - unsigned : NumExprBits; + uint64_t : NumExprBits; /// Is this a forced global delete, i.e. "::delete"? LLVM_PREFERRED_TYPE(bool) - unsigned GlobalDelete : 1; + uint64_t GlobalDelete : 1; /// Is this the array form of delete, i.e. "delete[]"? LLVM_PREFERRED_TYPE(bool) - unsigned ArrayForm : 1; + uint64_t ArrayForm : 1; /// ArrayFormAsWritten can be different from ArrayForm if 'delete' is /// applied to pointer-to-array type (ArrayFormAsWritten will be false /// while ArrayForm will be true). LLVM_PREFERRED_TYPE(bool) - unsigned ArrayFormAsWritten : 1; + uint64_t ArrayFormAsWritten : 1; /// Does the usual deallocation function for the element type require /// a size_t argument? LLVM_PREFERRED_TYPE(bool) - unsigned UsualArrayDeleteWantsSize : 1; + uint64_t UsualArrayDeleteWantsSize : 1; - /// Location of the expression. - SourceLocation Loc; + /// Location of the expression + LLVM_PREFERRED_TYPE(SourceLocation) + uint64_t Loc : SourceLocation::Bits; }; class TypeTraitExprBitfields { @@ -1032,7 +1048,7 @@ class alignas(void *) Stmt { LLVM_PREFERRED_TYPE(bool) unsigned IsImmediateEscalating : 1; - SourceLocation Loc; + }; class ExprWithCleanupsBitfields { @@ -1065,25 +1081,26 @@ class alignas(void *) Stmt { friend class CXXDependentScopeMemberExpr; LLVM_PREFERRED_TYPE(ExprBitfields) - unsigned : NumExprBits; + uint64_t : NumExprBits; /// Whether this member expression used the '->' operator or /// the '.' operator. LLVM_PREFERRED_TYPE(bool) - unsigned IsArrow : 1; + uint64_t IsArrow : 1; /// Whether this member expression has info for explicit template /// keyword and arguments. LLVM_PREFERRED_TYPE(bool) - unsigned HasTemplateKWAndArgsInfo : 1; + uint64_t HasTemplateKWAndArgsInfo : 1; /// See getFirstQualifierFoundInScope() and the comment listing /// the trailing objects. LLVM_PREFERRED_TYPE(bool) - unsigned HasFirstQualifierFoundInScope : 1; - + uint64_t HasFirstQualifierFoundInScope : 1; + /// The location of the '->' or '.' operator. - SourceLocation OperatorLoc; + LLVM_PREFERRED_TYPE(SourceLocation) + uint64_t OperatorLoc : SourceLocation::Bits; }; class OverloadExprBitfields { @@ -1158,12 +1175,13 @@ class alignas(void *) Stmt { class SubstNonTypeTemplateParmExprBitfields { friend class ASTStmtReader; friend class SubstNonTypeTemplateParmExpr; - + LLVM_PREFERRED_TYPE(ExprBitfields) - unsigned : NumExprBits; + uint64_t : NumExprBits; /// The location of the non-type template parameter reference. - SourceLocation NameLoc; + LLVM_PREFERRED_TYPE(SourceLocation) + uint64_t NameLoc : SourceLocation::Bits; }; class LambdaExprBitfields { @@ -1198,11 +1216,13 @@ class alignas(void *) Stmt { friend class RequiresExpr; LLVM_PREFERRED_TYPE(ExprBitfields) - unsigned : NumExprBits; + uint64_t : NumExprBits; LLVM_PREFERRED_TYPE(bool) - unsigned IsSatisfied : 1; - SourceLocation RequiresKWLoc; + uint64_t IsSatisfied : 1; + + LLVM_PREFERRED_TYPE(SourceLocation) + uint64_t RequiresKWLoc : SourceLocation::Bits; }; class ArrayTypeTraitExprBitfields { @@ -1287,14 +1307,16 @@ class alignas(void *) Stmt { friend class OpaqueValueExpr; LLVM_PREFERRED_TYPE(ExprBitfields) - unsigned : NumExprBits; + uint64_t : NumExprBits; /// The OVE is a unique semantic reference to its source expression if this /// bit is set to true. LLVM_PREFERRED_TYPE(bool) - unsigned IsUnique : 1; - - SourceLocation Loc; + uint64_t IsUnique : 1; + + /// The location of the non-type template parameter reference. + LLVM_PREFERRED_TYPE(SourceLocation) + uint64_t Loc : SourceLocation::Bits; }; class ConvertVectorExprBitfields { @@ -1702,8 +1724,12 @@ class NullStmt : public Stmt { /// Build an empty null statement. explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty) {} - SourceLocation getSemiLoc() const { return NullStmtBits.SemiLoc; } - void setSemiLoc(SourceLocation L) { NullStmtBits.SemiLoc = L; } + SourceLocation getSemiLoc() const { + return SourceLocation::getFromRawEncoding(NullStmtBits.SemiLoc); + } + void setSemiLoc(SourceLocation L) { + NullStmtBits.SemiLoc = L.getRawEncoding(); + } bool hasLeadingEmptyMacro() const { return NullStmtBits.HasLeadingEmptyMacro; @@ -1905,8 +1931,12 @@ class SwitchCase : public Stmt { SwitchCase *getNextSwitchCase() { return NextSwitchCase; } void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; } - SourceLocation getKeywordLoc() const { return SwitchCaseBits.KeywordLoc; } - void setKeywordLoc(SourceLocation L) { SwitchCaseBits.KeywordLoc = L; } + SourceLocation getKeywordLoc() const { + return SourceLocation::getFromRawEncoding(SwitchCaseBits.KeywordLoc); + } + void setKeywordLoc(SourceLocation L) { + SwitchCaseBits.KeywordLoc = L.getRawEncoding(); + } SourceLocation getColonLoc() const { return ColonLoc; } void setColonLoc(SourceLocation L) { ColonLoc = L; } @@ -1948,7 +1978,8 @@ class CaseStmt final // with a range. Present if and only if caseStmtIsGNURange() is true. enum { LhsOffset = 0, SubStmtOffsetFromRhs = 1 }; enum { NumMandatoryStmtPtr = 2 }; - + /// The location of the "case" or "default" keyword. + SourceLocation KeywordLoc; unsigned numTrailingObjects(OverloadToken) const { return NumMandatoryStmtPtr + caseStmtIsGNURange(); } @@ -2157,7 +2188,7 @@ class ValueStmt : public Stmt { class LabelStmt : public ValueStmt { LabelDecl *TheDecl; Stmt *SubStmt; - bool SideEntry = false; + bool SideEntry = false; // FIXME: could improve public: /// Build a label statement. @@ -2169,8 +2200,12 @@ class LabelStmt : public ValueStmt { /// Build an empty label statement. explicit LabelStmt(EmptyShell Empty) : ValueStmt(LabelStmtClass, Empty) {} - SourceLocation getIdentLoc() const { return LabelStmtBits.IdentLoc; } - void setIdentLoc(SourceLocation L) { LabelStmtBits.IdentLoc = L; } + SourceLocation getIdentLoc() const { + return SourceLocation::getFromRawEncoding(LabelStmtBits.IdentLoc); + } + void setIdentLoc(SourceLocation L) { + LabelStmtBits.IdentLoc = L.getRawEncoding(); + } LabelDecl *getDecl() const { return TheDecl; } void setDecl(LabelDecl *D) { TheDecl = D; } @@ -2206,21 +2241,22 @@ class AttributedStmt final private llvm::TrailingObjects { friend class ASTStmtReader; friend TrailingObjects; - + /// The location of the attribute. + SourceLocation AttrLoc; Stmt *SubStmt; AttributedStmt(SourceLocation Loc, ArrayRef Attrs, Stmt *SubStmt) : ValueStmt(AttributedStmtClass), SubStmt(SubStmt) { AttributedStmtBits.NumAttrs = Attrs.size(); - AttributedStmtBits.AttrLoc = Loc; - llvm::copy(Attrs, getAttrArrayPtr()); + AttrLoc = Loc; + std::copy(Attrs.begin(), Attrs.end(), getAttrArrayPtr()); } explicit AttributedStmt(EmptyShell Empty, unsigned NumAttrs) : ValueStmt(AttributedStmtClass, Empty) { AttributedStmtBits.NumAttrs = NumAttrs; - AttributedStmtBits.AttrLoc = SourceLocation{}; + AttrLoc = SourceLocation{}; std::fill_n(getAttrArrayPtr(), NumAttrs, nullptr); } @@ -2234,7 +2270,7 @@ class AttributedStmt final // Build an empty attributed statement. static AttributedStmt *CreateEmpty(const ASTContext &C, unsigned NumAttrs); - SourceLocation getAttrLoc() const { return AttributedStmtBits.AttrLoc; } + SourceLocation getAttrLoc() const { return AttrLoc; } ArrayRef getAttrs() const { return {getAttrArrayPtr(), AttributedStmtBits.NumAttrs}; } @@ -2425,8 +2461,12 @@ class IfStmt final getTrailingObjects()[initOffset()] = Init; } - SourceLocation getIfLoc() const { return IfStmtBits.IfLoc; } - void setIfLoc(SourceLocation IfLoc) { IfStmtBits.IfLoc = IfLoc; } + SourceLocation getIfLoc() const { + return SourceLocation::getFromRawEncoding(IfStmtBits.IfLoc); + } + void setIfLoc(SourceLocation IfLoc) { + IfStmtBits.IfLoc = IfLoc.getRawEncoding(); + } SourceLocation getElseLoc() const { return hasElseStorage() ? *getTrailingObjects() @@ -2511,7 +2551,6 @@ class IfStmt final class SwitchStmt final : public Stmt, private llvm::TrailingObjects { friend TrailingObjects; - /// Points to a linked list of case and default statements. SwitchCase *FirstCase = nullptr; @@ -2644,8 +2683,12 @@ class SwitchStmt final : public Stmt, const SwitchCase *getSwitchCaseList() const { return FirstCase; } void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; } - SourceLocation getSwitchLoc() const { return SwitchStmtBits.SwitchLoc; } - void setSwitchLoc(SourceLocation L) { SwitchStmtBits.SwitchLoc = L; } + SourceLocation getSwitchLoc() const { + return SourceLocation::getFromRawEncoding(SwitchStmtBits.SwitchLoc); + } + void setSwitchLoc(SourceLocation L) { + SwitchStmtBits.SwitchLoc = L.getRawEncoding(); + } SourceLocation getLParenLoc() const { return LParenLoc; } void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; } SourceLocation getRParenLoc() const { return RParenLoc; } @@ -2717,7 +2760,6 @@ class WhileStmt final : public Stmt, // enum { VarOffset = 0, BodyOffsetFromCond = 1 }; enum { NumMandatoryStmtPtr = 2 }; - SourceLocation LParenLoc, RParenLoc; unsigned varOffset() const { return VarOffset; } @@ -2802,8 +2844,12 @@ class WhileStmt final : public Stmt, getTrailingObjects()[varOffset()] = CondVar; } - SourceLocation getWhileLoc() const { return WhileStmtBits.WhileLoc; } - void setWhileLoc(SourceLocation L) { WhileStmtBits.WhileLoc = L; } + SourceLocation getWhileLoc() const { + return SourceLocation::getFromRawEncoding(WhileStmtBits.WhileLoc); + } + void setWhileLoc(SourceLocation L) { + WhileStmtBits.WhileLoc = L.getRawEncoding(); + } SourceLocation getLParenLoc() const { return LParenLoc; } void setLParenLoc(SourceLocation L) { LParenLoc = L; } @@ -2837,7 +2883,6 @@ class DoStmt : public Stmt { Stmt *SubExprs[END_EXPR]; SourceLocation WhileLoc; SourceLocation RParenLoc; // Location of final ')' in do stmt condition. - public: DoStmt(Stmt *Body, Expr *Cond, SourceLocation DL, SourceLocation WL, SourceLocation RP) @@ -2861,8 +2906,10 @@ class DoStmt : public Stmt { const Stmt *getBody() const { return SubExprs[BODY]; } void setBody(Stmt *Body) { SubExprs[BODY] = Body; } - SourceLocation getDoLoc() const { return DoStmtBits.DoLoc; } - void setDoLoc(SourceLocation L) { DoStmtBits.DoLoc = L; } + SourceLocation getDoLoc() const { + return SourceLocation::getFromRawEncoding(DoStmtBits.DoLoc); + } + void setDoLoc(SourceLocation L) { DoStmtBits.DoLoc = L.getRawEncoding(); } SourceLocation getWhileLoc() const { return WhileLoc; } void setWhileLoc(SourceLocation L) { WhileLoc = L; } SourceLocation getRParenLoc() const { return RParenLoc; } @@ -2944,8 +2991,10 @@ class ForStmt : public Stmt { void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast(E); } void setBody(Stmt *S) { SubExprs[BODY] = S; } - SourceLocation getForLoc() const { return ForStmtBits.ForLoc; } - void setForLoc(SourceLocation L) { ForStmtBits.ForLoc = L; } + SourceLocation getForLoc() const { + return SourceLocation::getFromRawEncoding(ForStmtBits.ForLoc); + } + void setForLoc(SourceLocation L) { ForStmtBits.ForLoc = L.getRawEncoding(); } SourceLocation getLParenLoc() const { return LParenLoc; } void setLParenLoc(SourceLocation L) { LParenLoc = L; } SourceLocation getRParenLoc() const { return RParenLoc; } @@ -2985,8 +3034,12 @@ class GotoStmt : public Stmt { LabelDecl *getLabel() const { return Label; } void setLabel(LabelDecl *D) { Label = D; } - SourceLocation getGotoLoc() const { return GotoStmtBits.GotoLoc; } - void setGotoLoc(SourceLocation L) { GotoStmtBits.GotoLoc = L; } + SourceLocation getGotoLoc() const { + return SourceLocation::getFromRawEncoding(GotoStmtBits.GotoLoc); + } + void setGotoLoc(SourceLocation L) { + GotoStmtBits.GotoLoc = L.getRawEncoding(); + } SourceLocation getLabelLoc() const { return LabelLoc; } void setLabelLoc(SourceLocation L) { LabelLoc = L; } @@ -3023,8 +3076,12 @@ class IndirectGotoStmt : public Stmt { explicit IndirectGotoStmt(EmptyShell Empty) : Stmt(IndirectGotoStmtClass, Empty) {} - void setGotoLoc(SourceLocation L) { GotoStmtBits.GotoLoc = L; } - SourceLocation getGotoLoc() const { return GotoStmtBits.GotoLoc; } + void setGotoLoc(SourceLocation L) { + GotoStmtBits.GotoLoc = L.getRawEncoding(); + } + SourceLocation getGotoLoc() const { + return SourceLocation::getFromRawEncoding(GotoStmtBits.GotoLoc); + } void setStarLoc(SourceLocation L) { StarLoc = L; } SourceLocation getStarLoc() const { return StarLoc; } @@ -3059,6 +3116,7 @@ class IndirectGotoStmt : public Stmt { /// ContinueStmt - This represents a continue. class ContinueStmt : public Stmt { public: + ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass) { setContinueLoc(CL); } @@ -3066,8 +3124,12 @@ class ContinueStmt : public Stmt { /// Build an empty continue statement. explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) {} - SourceLocation getContinueLoc() const { return ContinueStmtBits.ContinueLoc; } - void setContinueLoc(SourceLocation L) { ContinueStmtBits.ContinueLoc = L; } + SourceLocation getContinueLoc() const { + return SourceLocation::getFromRawEncoding(ContinueStmtBits.ContinueLoc); + } + void setContinueLoc(SourceLocation L) { + ContinueStmtBits.ContinueLoc = L.getRawEncoding(); + } SourceLocation getBeginLoc() const { return getContinueLoc(); } SourceLocation getEndLoc() const { return getContinueLoc(); } @@ -3096,8 +3158,12 @@ class BreakStmt : public Stmt { /// Build an empty break statement. explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) {} - SourceLocation getBreakLoc() const { return BreakStmtBits.BreakLoc; } - void setBreakLoc(SourceLocation L) { BreakStmtBits.BreakLoc = L; } + SourceLocation getBreakLoc() const { + return SourceLocation::getFromRawEncoding(BreakStmtBits.BreakLoc); + } + void setBreakLoc(SourceLocation L) { + BreakStmtBits.BreakLoc = L.getRawEncoding(); + } SourceLocation getBeginLoc() const { return getBreakLoc(); } SourceLocation getEndLoc() const { return getBreakLoc(); } @@ -3128,7 +3194,6 @@ class ReturnStmt final : public Stmt, private llvm::TrailingObjects { friend TrailingObjects; - /// The return expression. Stmt *RetExpr; @@ -3175,8 +3240,12 @@ class ReturnStmt final *getTrailingObjects() = Var; } - SourceLocation getReturnLoc() const { return ReturnStmtBits.RetLoc; } - void setReturnLoc(SourceLocation L) { ReturnStmtBits.RetLoc = L; } + SourceLocation getReturnLoc() const { + return SourceLocation::getFromRawEncoding(ReturnStmtBits.RetLoc); + } + void setReturnLoc(SourceLocation L) { + ReturnStmtBits.RetLoc = L.getRawEncoding(); + } SourceLocation getBeginLoc() const { return getReturnLoc(); } SourceLocation getEndLoc() const LLVM_READONLY { diff --git a/clang/include/clang/Basic/Diagnostic.h b/clang/include/clang/Basic/Diagnostic.h index 7ae4ef7df138c..8fdf1bc01bdd5 100644 --- a/clang/include/clang/Basic/Diagnostic.h +++ b/clang/include/clang/Basic/Diagnostic.h @@ -454,9 +454,9 @@ class DiagnosticsEngine : public RefCountedBase { /// modifications done through the command-line. struct DiagStatePoint { DiagState *State; - unsigned Offset; + SourceLocation::UIntTy Offset; - DiagStatePoint(DiagState *State, unsigned Offset) + DiagStatePoint(DiagState *State, SourceLocation::UIntTy Offset) : State(State), Offset(Offset) {} }; @@ -469,7 +469,7 @@ class DiagnosticsEngine : public RefCountedBase { File *Parent = nullptr; /// The offset of this file within its parent. - unsigned ParentOffset = 0; + SourceLocation::UIntTy ParentOffset = 0; /// Whether this file has any local (not imported from an AST file) /// diagnostic state transitions. @@ -479,7 +479,7 @@ class DiagnosticsEngine : public RefCountedBase { /// be at least one of these (the state on entry to the file). llvm::SmallVector StateTransitions; - DiagState *lookup(unsigned Offset) const; + DiagState *lookup(SourceLocation::UIntTy Offset) const; }; /// The diagnostic states for each file. diff --git a/clang/include/clang/Basic/SourceLocation.h b/clang/include/clang/Basic/SourceLocation.h index 14543cc41a38e..91352a4aa0ab6 100644 --- a/clang/include/clang/Basic/SourceLocation.h +++ b/clang/include/clang/Basic/SourceLocation.h @@ -17,6 +17,7 @@ #include "clang/Basic/FileEntry.h" #include "clang/Basic/LLVM.h" #include "llvm/ADT/StringRef.h" +#include "llvm/Support/MathExtras.h" #include #include #include @@ -70,8 +71,6 @@ class FileID { int getOpaqueValue() const { return ID; } }; -using FileIDAndOffset = std::pair; - /// Encodes a location in the source. The SourceManager can decode this /// to get at the full include stack, line and column information. /// @@ -95,13 +94,14 @@ class SourceLocation { friend class SourceLocationEncoding; public: - using UIntTy = uint32_t; - using IntTy = int32_t; + using UIntTy = uint64_t; + using IntTy = int64_t; + static constexpr unsigned Bits = 40; private: - UIntTy ID = 0; + uint64_t ID = 0; - enum : UIntTy { MacroIDBit = 1ULL << (8 * sizeof(UIntTy) - 1) }; + enum : UIntTy { MacroIDBit = 1ULL << (Bits - 1) }; public: bool isFileID() const { return (ID & MacroIDBit) == 0; } @@ -160,6 +160,24 @@ class SourceLocation { return X; } + static SourceLocation getFromRawEncoding32(const SourceManager &SM, + uint32_t Encoding32); + + bool getRawEncoding32(uint32_t &Result) const { + // A mask that isolates this check to the required range higher of bits. + static constexpr uint64_t RangeMask = llvm::maskTrailingOnes(Bits - 32) << 31; + + // Check if the ID can be safely compressed to a 32-bit integer. + // The truncation is only possible if all higher bits of the ID are all identical: + // all 0s for the local offset, or all 1s for loaded offset + if ((ID ^ (ID << 1)) & RangeMask) + return false; // won't fit + uint32_t Lower31Bits = ID & llvm::maskTrailingOnes(31); + // Restore the top macro bit. + Result = Lower31Bits | ((ID & MacroIDBit) >> (Bits - 32)); + return true; + } + /// When a SourceLocation itself cannot be used, this returns /// an (opaque) pointer encoding for it. /// @@ -210,6 +228,7 @@ inline bool operator<=(const SourceLocation &LHS, const SourceLocation &RHS) { inline bool operator>=(const SourceLocation &LHS, const SourceLocation &RHS) { return LHS.getRawEncoding() >= RHS.getRawEncoding(); } +using FileIDAndOffset = std::pair; /// A trivial tuple used to represent a source range. class SourceRange { diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h index eefd4885534c8..930e48d6c6d84 100644 --- a/clang/include/clang/Basic/SourceManager.h +++ b/clang/include/clang/Basic/SourceManager.h @@ -720,6 +720,8 @@ class SourceManager : public RefCountedBase { /// expansion. SmallVector LocalSLocEntryTable; + SmallVector LocalLocOffsetTable; + /// The table of SLocEntries that are loaded from other modules. /// /// Negative FileIDs are indexes into this table. To get from ID to an index, @@ -745,7 +747,7 @@ class SourceManager : public RefCountedBase { /// The highest possible offset is 2^31-1 (2^63-1 for 64-bit source /// locations), so CurrentLoadedOffset starts at 2^31 (2^63 resp.). static const SourceLocation::UIntTy MaxLoadedOffset = - 1ULL << (8 * sizeof(SourceLocation::UIntTy) - 1); + 1ULL << (SourceLocation::Bits - 1); /// A bitmap that indicates whether the entries of LoadedSLocEntryTable /// have already been loaded from the external source. @@ -1255,7 +1257,8 @@ class SourceManager : public RefCountedBase { SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const; /// Form a SourceLocation from a FileID and Offset pair. - SourceLocation getComposedLoc(FileID FID, unsigned Offset) const { + SourceLocation getComposedLoc(FileID FID, + SourceLocation::UIntTy Offset) const { auto *Entry = getSLocEntryOrNull(FID); if (!Entry) return SourceLocation(); @@ -1287,7 +1290,7 @@ class SourceManager : public RefCountedBase { if (!E) return std::make_pair(FileID(), 0); - unsigned Offset = Loc.getOffset()-E->getOffset(); + auto Offset = Loc.getOffset()-E->getOffset(); if (Loc.isFileID()) return std::make_pair(FID, Offset); @@ -1304,7 +1307,7 @@ class SourceManager : public RefCountedBase { if (!E) return std::make_pair(FileID(), 0); - unsigned Offset = Loc.getOffset()-E->getOffset(); + auto Offset = Loc.getOffset()-E->getOffset(); if (Loc.isFileID()) return std::make_pair(FID, Offset); return getDecomposedSpellingLocSlowCase(E, Offset); @@ -1318,7 +1321,7 @@ class SourceManager : public RefCountedBase { /// specified SourceLocation represents. /// /// This is not very meaningful for a macro ID. - unsigned getFileOffset(SourceLocation SpellingLoc) const { + SourceLocation::UIntTy getFileOffset(SourceLocation SpellingLoc) const { return getDecomposedLoc(SpellingLoc).second; } @@ -1979,7 +1982,7 @@ class SourceManager : public RefCountedBase { FileIDAndOffset getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const; FileIDAndOffset getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E, - unsigned Offset) const; + SourceLocation::UIntTy Offset) const; void computeMacroArgsCache(MacroArgsMap &MacroArgsCache, FileID FID) const; void associateFileChunkWithMacroArgExp(MacroArgsMap &MacroArgsCache, FileID FID, diff --git a/clang/include/clang/Rewrite/Core/Rewriter.h b/clang/include/clang/Rewrite/Core/Rewriter.h index 4e96f6fcca919..02f9f2a60240b 100644 --- a/clang/include/clang/Rewrite/Core/Rewriter.h +++ b/clang/include/clang/Rewrite/Core/Rewriter.h @@ -216,7 +216,8 @@ class Rewriter { bool overwriteChangedFiles(); private: - unsigned getLocationOffsetAndFileID(SourceLocation Loc, FileID &FID) const; + SourceLocation::UIntTy getLocationOffsetAndFileID(SourceLocation Loc, + FileID &FID) const; }; } // namespace clang diff --git a/clang/include/clang/Sema/MultiplexExternalSemaSource.h b/clang/include/clang/Sema/MultiplexExternalSemaSource.h index 7c66c26a17a13..449166881d207 100644 --- a/clang/include/clang/Sema/MultiplexExternalSemaSource.h +++ b/clang/include/clang/Sema/MultiplexExternalSemaSource.h @@ -72,7 +72,7 @@ class MultiplexExternalSemaSource : public ExternalSemaSource { void CompleteRedeclChain(const Decl *D) override; /// Resolve a selector ID into a selector. - Selector GetExternalSelector(uint32_t ID) override; + Selector GetExternalSelector(uint64_t ID) override; /// Returns the number of selectors known to the external AST /// source. diff --git a/clang/include/clang/Serialization/ASTBitCodes.h b/clang/include/clang/Serialization/ASTBitCodes.h index 9d265f27b8e31..ed92a9319742f 100644 --- a/clang/include/clang/Serialization/ASTBitCodes.h +++ b/clang/include/clang/Serialization/ASTBitCodes.h @@ -164,7 +164,7 @@ using LocalMacroID = uint32_t; const unsigned int NUM_PREDEF_MACRO_IDS = 1; /// An ID number that refers to an ObjC selector in an AST file. -using SelectorID = uint32_t; +using SelectorID = uint64_t; /// The number of predefined selector IDs. const unsigned int NUM_PREDEF_SELECTOR_IDS = 1; diff --git a/clang/include/clang/Serialization/ASTReader.h b/clang/include/clang/Serialization/ASTReader.h index 7d4b4467eb97d..43b4576093cf4 100644 --- a/clang/include/clang/Serialization/ASTReader.h +++ b/clang/include/clang/Serialization/ASTReader.h @@ -525,7 +525,7 @@ class ASTReader ContinuousRangeMap GlobalSLocEntryMap; using GlobalSLocOffsetMapType = - ContinuousRangeMap; + ContinuousRangeMap; /// A map of reversed (SourceManager::MaxLoadedOffset - SLocOffset) /// SourceLocation offsets to the modules containing them. diff --git a/clang/include/clang/Serialization/SourceLocationEncoding.h b/clang/include/clang/Serialization/SourceLocationEncoding.h index 5b2485dbc719f..e18e1e2d042fd 100644 --- a/clang/include/clang/Serialization/SourceLocationEncoding.h +++ b/clang/include/clang/Serialization/SourceLocationEncoding.h @@ -46,10 +46,12 @@ class SourceLocationEncoding { constexpr static unsigned UIntBits = CHAR_BIT * sizeof(UIntTy); static UIntTy encodeRaw(UIntTy Raw) { - return (Raw << 1) | (Raw >> (UIntBits - 1)); + return ((Raw & llvm::maskTrailingOnes(SourceLocation::Bits - 1)) + << 1) | + (Raw >> (SourceLocation::Bits - 1)); } static UIntTy decodeRaw(UIntTy Raw) { - return (Raw >> 1) | (Raw << (UIntBits - 1)); + return (Raw >> 1) | ((Raw & 1) << (SourceLocation::Bits - 1)); } public: @@ -79,18 +81,18 @@ SourceLocationEncoding::encode(SourceLocation Loc, UIntTy BaseOffset, // 16 bits should be sufficient to store the module file index. assert(BaseModuleFileIndex < (1 << 16)); - Encoded |= (RawLocEncoding)BaseModuleFileIndex << 32; + Encoded |= (RawLocEncoding)BaseModuleFileIndex << (SourceLocation::Bits + 1); return Encoded; } inline std::pair SourceLocationEncoding::decode(RawLocEncoding Encoded) { - unsigned ModuleFileIndex = Encoded >> 32; + unsigned ModuleFileIndex = Encoded >> (SourceLocation::Bits + 1); if (!ModuleFileIndex) return {SourceLocation::getFromRawEncoding(decodeRaw(Encoded)), ModuleFileIndex}; - Encoded &= llvm::maskTrailingOnes(32); + Encoded &= llvm::maskTrailingOnes((SourceLocation::Bits + 1)); SourceLocation Loc = SourceLocation::getFromRawEncoding(decodeRaw(Encoded)); return {Loc, ModuleFileIndex}; diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp index b13bdd5642977..689def9c6c38f 100644 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -3200,6 +3200,15 @@ TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, return DI; } +CXXOperatorSourceInfo * +ASTContext::getCXXOperatorSourceInfo(SourceRange R) const { + auto *TInfo = (CXXOperatorSourceInfo *)BumpAlloc.Allocate( + sizeof(CXXOperatorSourceInfo), 8); + TInfo->BeginOpNameLoc = R.getBegin(); + TInfo->EndOpNameLoc = R.getEnd(); + return TInfo; +} + const ASTRecordLayout & ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { return getObjCLayout(D); @@ -7095,8 +7104,7 @@ ASTContext::getNameForTemplate(TemplateName Name, } else { DName = DeclarationNames.getCXXOperatorName(TN.getOperator()); // DNInfo work in progress: FIXME: source locations? - DeclarationNameLoc DNLoc = - DeclarationNameLoc::makeCXXOperatorNameLoc(SourceRange()); + DeclarationNameLoc DNLoc = DeclarationNameLoc::makeCXXOperatorNameLoc(nullptr); return DeclarationNameInfo(DName, NameLoc, DNLoc); } } diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp index 90d309e49b264..9617c8b1d6af0 100644 --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -2071,7 +2071,8 @@ ASTNodeImporter::ImportDeclarationNameLoc( case DeclarationName::CXXOperatorName: { if (auto ToRangeOrErr = import(From.getCXXOperatorNameRange())) - To.setCXXOperatorNameRange(*ToRangeOrErr); + To.setCXXOperatorNameInfo( + Importer.ToContext.getCXXOperatorSourceInfo(*ToRangeOrErr)); else return ToRangeOrErr.takeError(); return Error::success(); diff --git a/clang/lib/AST/DeclarationName.cpp b/clang/lib/AST/DeclarationName.cpp index ae5fcf6e86adf..430fa9372b3cf 100644 --- a/clang/lib/AST/DeclarationName.cpp +++ b/clang/lib/AST/DeclarationName.cpp @@ -387,7 +387,7 @@ DeclarationNameLoc::DeclarationNameLoc(DeclarationName Name) { setNamedTypeLoc(nullptr); break; case DeclarationName::CXXOperatorName: - setCXXOperatorNameRange(SourceRange()); + CXXOperatorName.OInfo = nullptr; break; case DeclarationName::CXXLiteralOperatorName: setCXXLiteralOperatorNameLoc(SourceLocation()); diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp index 642867c0942b5..9838d5bcd093b 100644 --- a/clang/lib/AST/Expr.cpp +++ b/clang/lib/AST/Expr.cpp @@ -441,7 +441,7 @@ DeclRefExpr::DeclRefExpr(const ASTContext &Ctx, ValueDecl *D, DeclRefExprBits.CapturedByCopyInLambdaWithExplicitObjectParameter = false; DeclRefExprBits.NonOdrUseReason = NOUR; DeclRefExprBits.IsImmediateEscalating = false; - DeclRefExprBits.Loc = L; + Loc = L; setDependence(computeDependence(this, Ctx)); } @@ -454,7 +454,7 @@ DeclRefExpr::DeclRefExpr(const ASTContext &Ctx, QualType T, ExprValueKind VK, NonOdrUseReason NOUR) : Expr(DeclRefExprClass, T, VK, OK_Ordinary), D(D), DNLoc(NameInfo.getInfo()) { - DeclRefExprBits.Loc = NameInfo.getLoc(); + Loc = NameInfo.getLoc(); DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0; if (QualifierLoc) new (getTrailingObjects()) @@ -618,7 +618,7 @@ PredefinedExpr::PredefinedExpr(SourceLocation L, QualType FNTy, bool HasFunctionName = SL != nullptr; PredefinedExprBits.HasFunctionName = HasFunctionName; PredefinedExprBits.IsTransparent = IsTransparent; - PredefinedExprBits.Loc = L; + Loc = L; if (HasFunctionName) setFunctionName(SL); setDependence(computeDependence(this)); @@ -1466,15 +1466,15 @@ static unsigned SizeOfCallExprInstance(Expr::StmtClass SC) { // changing the size of SourceLocation, CallExpr, and // subclasses requires careful considerations -static_assert(sizeof(SourceLocation) == 4 && sizeof(CXXOperatorCallExpr) <= 32, - "we assume CXXOperatorCallExpr is at most 32 bytes"); +static_assert(sizeof(SourceLocation) == 8 && sizeof(CXXOperatorCallExpr) <= 40, + "we assume CXXOperatorCallExpr is at most 40 bytes"); CallExpr::CallExpr(StmtClass SC, Expr *Fn, ArrayRef PreArgs, ArrayRef Args, QualType Ty, ExprValueKind VK, SourceLocation RParenLoc, FPOptionsOverride FPFeatures, unsigned MinNumArgs, ADLCallKind UsesADL) : Expr(SC, Ty, VK, OK_Ordinary), RParenLoc(RParenLoc) { - NumArgs = std::max(Args.size(), MinNumArgs); + CallExprBits.NumArgs = std::max(Args.size(), MinNumArgs); unsigned NumPreArgs = PreArgs.size(); CallExprBits.NumPreArgs = NumPreArgs; assert((NumPreArgs == getNumPreArgs()) && "NumPreArgs overflow!"); @@ -1488,7 +1488,7 @@ CallExpr::CallExpr(StmtClass SC, Expr *Fn, ArrayRef PreArgs, setPreArg(I, PreArgs[I]); for (unsigned I = 0; I != Args.size(); ++I) setArg(I, Args[I]); - for (unsigned I = Args.size(); I != NumArgs; ++I) + for (unsigned I = Args.size(); I != CallExprBits.NumArgs; ++I) setArg(I, nullptr); this->computeDependence(); @@ -1504,7 +1504,8 @@ CallExpr::CallExpr(StmtClass SC, Expr *Fn, ArrayRef PreArgs, CallExpr::CallExpr(StmtClass SC, unsigned NumPreArgs, unsigned NumArgs, bool HasFPFeatures, EmptyShell Empty) - : Expr(SC, Empty), NumArgs(NumArgs) { + : Expr(SC, Empty) { + CallExprBits.NumArgs = NumArgs; CallExprBits.NumPreArgs = NumPreArgs; assert((NumPreArgs == getNumPreArgs()) && "NumPreArgs overflow!"); CallExprBits.HasFPFeatures = HasFPFeatures; @@ -1727,7 +1728,7 @@ MemberExpr::MemberExpr(Expr *Base, bool IsArrow, SourceLocation OperatorLoc, TemplateArgs || TemplateKWLoc.isValid(); MemberExprBits.HadMultipleCandidates = false; MemberExprBits.NonOdrUseReason = NOUR; - MemberExprBits.OperatorLoc = OperatorLoc; + this->OperatorLoc = OperatorLoc; if (hasQualifier()) new (getTrailingObjects()) @@ -4421,7 +4422,7 @@ GenericSelectionExpr::GenericSelectionExpr( " and TypeSourceInfo!"); assert(ResultIndex < NumAssocs && "ResultIndex is out-of-bounds!"); - GenericSelectionExprBits.GenericLoc = GenericLoc; + GenericSelectionExprBits.GenericLoc = GenericLoc.getRawEncoding(); getTrailingObjects()[getIndexOfControllingExpression()] = ControllingExpr; llvm::copy(AssocExprs, @@ -4448,7 +4449,7 @@ GenericSelectionExpr::GenericSelectionExpr( " and TypeSourceInfo!"); assert(ResultIndex < NumAssocs && "ResultIndex is out-of-bounds!"); - GenericSelectionExprBits.GenericLoc = GenericLoc; + GenericSelectionExprBits.GenericLoc = GenericLoc.getRawEncoding(); getTrailingObjects()[getIndexOfControllingType()] = ControllingType; llvm::copy(AssocExprs, @@ -4472,7 +4473,7 @@ GenericSelectionExpr::GenericSelectionExpr( "Must have the same number of association expressions" " and TypeSourceInfo!"); - GenericSelectionExprBits.GenericLoc = GenericLoc; + GenericSelectionExprBits.GenericLoc = GenericLoc.getRawEncoding(); getTrailingObjects()[getIndexOfControllingExpression()] = ControllingExpr; llvm::copy(AssocExprs, @@ -4496,7 +4497,7 @@ GenericSelectionExpr::GenericSelectionExpr( "Must have the same number of association expressions" " and TypeSourceInfo!"); - GenericSelectionExprBits.GenericLoc = GenericLoc; + GenericSelectionExprBits.GenericLoc = GenericLoc.getRawEncoding(); getTrailingObjects()[getIndexOfControllingType()] = ControllingType; llvm::copy(AssocExprs, @@ -4851,7 +4852,7 @@ BinaryOperator::BinaryOperator(const ASTContext &Ctx, Expr *lhs, Expr *rhs, BinaryOperatorBits.Opc = opc; assert(!isCompoundAssignmentOp() && "Use CompoundAssignOperator for compound assignments"); - BinaryOperatorBits.OpLoc = opLoc; + this->OpLoc = opLoc; BinaryOperatorBits.ExcludedOverflowPattern = false; SubExprs[LHS] = lhs; SubExprs[RHS] = rhs; @@ -4871,7 +4872,7 @@ BinaryOperator::BinaryOperator(const ASTContext &Ctx, Expr *lhs, Expr *rhs, BinaryOperatorBits.ExcludedOverflowPattern = false; assert(isCompoundAssignmentOp() && "Use CompoundAssignOperator for compound assignments"); - BinaryOperatorBits.OpLoc = opLoc; + this->OpLoc = opLoc; SubExprs[LHS] = lhs; SubExprs[RHS] = rhs; BinaryOperatorBits.HasFPFeatures = FPFeatures.requiresTrailingStorage(); @@ -4938,7 +4939,7 @@ UnaryOperator::UnaryOperator(const ASTContext &Ctx, Expr *input, Opcode opc, : Expr(UnaryOperatorClass, type, VK, OK), Val(input) { UnaryOperatorBits.Opc = opc; UnaryOperatorBits.CanOverflow = CanOverflow; - UnaryOperatorBits.Loc = l; + this->Loc = l; UnaryOperatorBits.HasFPFeatures = FPFeatures.requiresTrailingStorage(); if (hasStoredFPFeatures()) setStoredFPFeatures(FPFeatures); diff --git a/clang/lib/AST/ExprCXX.cpp b/clang/lib/AST/ExprCXX.cpp index 764e20a54dc89..0a46aa46fb4c0 100644 --- a/clang/lib/AST/ExprCXX.cpp +++ b/clang/lib/AST/ExprCXX.cpp @@ -1060,7 +1060,7 @@ CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &Ctx, : VK_PRValue, /*FIXME*/ OK_Ordinary), Field(Field), UsedContext(UsedContext) { - CXXDefaultInitExprBits.Loc = Loc; + CXXDefaultInitExprBits.Loc = Loc.getRawEncoding(); CXXDefaultInitExprBits.HasRewrittenInit = RewrittenInitExpr != nullptr; if (CXXDefaultInitExprBits.HasRewrittenInit) @@ -1204,7 +1204,7 @@ CXXConstructExpr::CXXConstructExpr( CXXConstructExprBits.ZeroInitialization = ZeroInitialization; CXXConstructExprBits.ConstructionKind = llvm::to_underlying(ConstructKind); CXXConstructExprBits.IsImmediateEscalating = false; - CXXConstructExprBits.Loc = Loc; + this->Loc = Loc; Stmt **TrailingArgs = getTrailingArgs(); llvm::copy(Args, TrailingArgs); @@ -1508,7 +1508,7 @@ CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr( (TemplateArgs != nullptr) || TemplateKWLoc.isValid(); CXXDependentScopeMemberExprBits.HasFirstQualifierFoundInScope = FirstQualifierFoundInScope != nullptr; - CXXDependentScopeMemberExprBits.OperatorLoc = OperatorLoc; + CXXDependentScopeMemberExprBits.OperatorLoc = OperatorLoc.getRawEncoding(); if (TemplateArgs) { auto Deps = TemplateArgumentDependence::None; diff --git a/clang/lib/AST/ExprConcepts.cpp b/clang/lib/AST/ExprConcepts.cpp index ac0e566fe6e72..35736fdd576ce 100644 --- a/clang/lib/AST/ExprConcepts.cpp +++ b/clang/lib/AST/ExprConcepts.cpp @@ -122,7 +122,7 @@ RequiresExpr::RequiresExpr(ASTContext &C, SourceLocation RequiresKWLoc, NumRequirements(Requirements.size()), Body(Body), LParenLoc(LParenLoc), RParenLoc(RParenLoc), RBraceLoc(RBraceLoc) { RequiresExprBits.IsSatisfied = false; - RequiresExprBits.RequiresKWLoc = RequiresKWLoc; + RequiresExprBits.RequiresKWLoc = RequiresKWLoc.getRawEncoding(); bool Dependent = false; bool ContainsUnexpandedParameterPack = false; for (ParmVarDecl *P : LocalParameters) { diff --git a/clang/lib/AST/ExternalASTSource.cpp b/clang/lib/AST/ExternalASTSource.cpp index e8c1004089713..9f714f8a8d455 100644 --- a/clang/lib/AST/ExternalASTSource.cpp +++ b/clang/lib/AST/ExternalASTSource.cpp @@ -72,7 +72,7 @@ bool ExternalASTSource::layoutRecordType( Decl *ExternalASTSource::GetExternalDecl(GlobalDeclID ID) { return nullptr; } -Selector ExternalASTSource::GetExternalSelector(uint32_t ID) { +Selector ExternalASTSource::GetExternalSelector(uint64_t ID) { return Selector(); } diff --git a/clang/lib/AST/Stmt.cpp b/clang/lib/AST/Stmt.cpp index 4fc4a99ad2405..41c53f9be6027 100644 --- a/clang/lib/AST/Stmt.cpp +++ b/clang/lib/AST/Stmt.cpp @@ -1072,7 +1072,7 @@ ForStmt::ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar, SubExprs[COND] = Cond; SubExprs[INC] = Inc; SubExprs[BODY] = Body; - ForStmtBits.ForLoc = FL; + ForStmtBits.ForLoc = FL.getRawEncoding(); } VarDecl *ForStmt::getConditionVariable() const { diff --git a/clang/lib/Basic/Diagnostic.cpp b/clang/lib/Basic/Diagnostic.cpp index ab0525e96f3ba..9e176c1bc7b1d 100644 --- a/clang/lib/Basic/Diagnostic.cpp +++ b/clang/lib/Basic/Diagnostic.cpp @@ -205,7 +205,7 @@ DiagnosticsEngine::DiagStateMap::lookup(SourceManager &SrcMgr, } DiagnosticsEngine::DiagState * -DiagnosticsEngine::DiagStateMap::File::lookup(unsigned Offset) const { +DiagnosticsEngine::DiagStateMap::File::lookup(SourceLocation::UIntTy Offset) const { auto OnePastIt = llvm::partition_point(StateTransitions, [=](const DiagStatePoint &P) { return P.Offset <= Offset; diff --git a/clang/lib/Basic/SourceLocation.cpp b/clang/lib/Basic/SourceLocation.cpp index 3e26f75d25b10..2a4e9f3e70681 100644 --- a/clang/lib/Basic/SourceLocation.cpp +++ b/clang/lib/Basic/SourceLocation.cpp @@ -17,6 +17,8 @@ #include "llvm/ADT/DenseMapInfo.h" #include "llvm/ADT/FoldingSet.h" #include "llvm/Support/Compiler.h" +#include "llvm/Support/MathExtras.h" +#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/raw_ostream.h" #include #include @@ -48,6 +50,27 @@ static_assert(std::is_trivially_destructible_v, "SourceRange must be trivially destructible because it is " "used in unions"); +SourceLocation SourceLocation::getFromRawEncoding32(const SourceManager &SM, + uint32_t Encoding32) { + uint32_t Lower31Bits = Encoding32 & llvm::maskTrailingOnes(31); + uint64_t MacroBit = + (static_cast(Encoding32) & llvm::maskLeadingOnes(1)) + << (Bits - 32); + + if (Lower31Bits < SM.getNextLocalOffset()) { + // This is local offset, 32-to-64 offset mapping is identical. + UIntTy Raw64 = Lower31Bits | MacroBit; + return getFromRawEncoding(Raw64); + } + // Offset of loaded source location. + // 2^63 -> 2^31 + // 2^63 - 1 -> 2^31 - 1 + static constexpr uint64_t RangeMask = + llvm::maskTrailingOnes(Bits - 32) << 31; + UIntTy Raw64 = (RangeMask + Lower31Bits) | MacroBit; + return getFromRawEncoding(Raw64); +} + unsigned SourceLocation::getHashValue() const { return llvm::DenseMapInfo::getHashValue(ID); } diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp index d007da0a00608..b7cbcd54b8e19 100644 --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -329,6 +329,7 @@ void SourceManager::clearIDTables() { MainFileID = FileID(); LocalSLocEntryTable.clear(); LoadedSLocEntryTable.clear(); + LocalLocOffsetTable.clear(); SLocEntryLoaded.clear(); SLocEntryOffsetLoaded.clear(); LastLineNoFileIDQuery = FileID(); @@ -636,9 +637,11 @@ FileID SourceManager::createFileIDImpl(ContentCache &File, StringRef Filename, noteSLocAddressSpaceUsage(Diag); return FileID(); } + assert(LocalSLocEntryTable.size() == LocalLocOffsetTable.size()); LocalSLocEntryTable.push_back( SLocEntry::get(NextLocalOffset, FileInfo::get(IncludePos, File, FileCharacter, Filename))); + LocalLocOffsetTable.push_back(NextLocalOffset); // We do a +1 here because we want a SourceLocation that means "the end of the // file", e.g. for the "no newline at the end of the file" diagnostic. NextLocalOffset += FileSize + 1; @@ -690,7 +693,9 @@ SourceManager::createExpansionLocImpl(const ExpansionInfo &Info, SLocEntryLoaded[Index] = SLocEntryOffsetLoaded[Index] = true; return SourceLocation::getMacroLoc(LoadedOffset); } + assert(LocalSLocEntryTable.size() == LocalLocOffsetTable.size()); LocalSLocEntryTable.push_back(SLocEntry::get(NextLocalOffset, Info)); + LocalLocOffsetTable.push_back(NextLocalOffset); if (NextLocalOffset + Length + 1 <= NextLocalOffset || NextLocalOffset + Length + 1 > CurrentLoadedOffset) { Diag.Report(diag::err_sloc_space_too_large); @@ -829,10 +834,11 @@ FileID SourceManager::getFileIDLocal(SourceLocation::UIntTy SLocOffset) const { // SLocOffset. unsigned LessIndex = 0; // upper bound of the search range. - unsigned GreaterIndex = LocalSLocEntryTable.size(); + assert(LocalSLocEntryTable.size() == LocalLocOffsetTable.size()); + unsigned GreaterIndex = LocalLocOffsetTable.size(); if (LastFileIDLookup.ID >= 0) { // Use the LastFileIDLookup to prune the search space. - if (LocalSLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) + if (LocalLocOffsetTable[LastFileIDLookup.ID] < SLocOffset) LessIndex = LastFileIDLookup.ID; else GreaterIndex = LastFileIDLookup.ID; @@ -843,7 +849,7 @@ FileID SourceManager::getFileIDLocal(SourceLocation::UIntTy SLocOffset) const { while (true) { --GreaterIndex; assert(GreaterIndex < LocalSLocEntryTable.size()); - if (LocalSLocEntryTable[GreaterIndex].getOffset() <= SLocOffset) { + if (LocalLocOffsetTable[GreaterIndex] <= SLocOffset) { FileID Res = FileID::get(int(GreaterIndex)); // Remember it. We have good locality across FileID lookups. LastFileIDLookup = Res; @@ -854,35 +860,23 @@ FileID SourceManager::getFileIDLocal(SourceLocation::UIntTy SLocOffset) const { break; } - NumProbes = 0; - while (true) { - unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex; - SourceLocation::UIntTy MidOffset = - getLocalSLocEntry(MiddleIndex).getOffset(); + while (LessIndex < GreaterIndex) { + ++NumBinaryProbes; - ++NumProbes; + unsigned MiddleIndex = LessIndex + (GreaterIndex - LessIndex) / 2; - // If the offset of the midpoint is too large, chop the high side of the - // range to the midpoint. - if (MidOffset > SLocOffset) { - GreaterIndex = MiddleIndex; - continue; - } - - // If the middle index contains the value, succeed and return. - if (MiddleIndex + 1 == LocalSLocEntryTable.size() || - SLocOffset < getLocalSLocEntry(MiddleIndex + 1).getOffset()) { - FileID Res = FileID::get(MiddleIndex); - - // Remember it. We have good locality across FileID lookups. - LastFileIDLookup = Res; - NumBinaryProbes += NumProbes; - return Res; - } + SourceLocation::UIntTy MidOffset = LocalLocOffsetTable[MiddleIndex]; - // Otherwise, move the low-side up to the middle index. - LessIndex = MiddleIndex; + if (MidOffset <= SLocOffset) + LessIndex = MiddleIndex + 1; + else + GreaterIndex = MiddleIndex; } + + // The loop terminates when LessIndex == GreaterIndex. At this point, + // `LessIndex` is the index of the *first element greater than* SLocOffset. + // The element we are actually looking for is the one immediately before it. + return LastFileIDLookup = FileID::get(LessIndex - 1); } /// Return the FileID for a SourceLocation with a high offset. @@ -937,7 +931,7 @@ FileIDAndOffset SourceManager::getDecomposedExpansionLocSlowCase( // If this is an expansion record, walk through all the expansion points. FileID FID; SourceLocation Loc; - unsigned Offset; + SourceLocation::UIntTy Offset; do { Loc = E->getExpansion().getExpansionLocStart(); @@ -951,7 +945,7 @@ FileIDAndOffset SourceManager::getDecomposedExpansionLocSlowCase( FileIDAndOffset SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E, - unsigned Offset) const { + SourceLocation::UIntTy Offset) const { // If this is an expansion record, walk through all the expansion points. FileID FID; SourceLocation Loc; @@ -1886,9 +1880,7 @@ SourceManager::getMacroArgExpandedLocation(SourceLocation Loc) const { if (Loc.isInvalid() || !Loc.isFileID()) return Loc; - FileID FID; - unsigned Offset; - std::tie(FID, Offset) = getDecomposedLoc(Loc); + auto [FID, Offset] = getDecomposedLoc(Loc); if (FID.isInvalid()) return Loc; diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp index 06f68ec8b0fc1..9b0dda81f02a8 100644 --- a/clang/lib/Format/FormatTokenLexer.cpp +++ b/clang/lib/Format/FormatTokenLexer.cpp @@ -1243,7 +1243,8 @@ FormatToken *FormatTokenLexer::getNextToken() { FormatTok = new (Allocator.Allocate()) FormatToken; readRawToken(*FormatTok); SourceLocation WhitespaceStart = - FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace); + FormatTok->Tok.getLocation().getLocWithOffset( + -static_cast(TrailingWhitespace)); FormatTok->IsFirst = IsFirstToken; IsFirstToken = false; diff --git a/clang/lib/Parse/ParseStmtAsm.cpp b/clang/lib/Parse/ParseStmtAsm.cpp index c679aa6fe7b27..9c1e497867a6e 100644 --- a/clang/lib/Parse/ParseStmtAsm.cpp +++ b/clang/lib/Parse/ParseStmtAsm.cpp @@ -183,7 +183,8 @@ ClangAsmParserCallback::translateLocation(const llvm::SourceMgr &LSM, if (TokIndex < AsmToks.size()) { const Token &Tok = AsmToks[TokIndex]; Loc = Tok.getLocation(); - Loc = Loc.getLocWithOffset(Offset - TokOffset); + Loc = Loc.getLocWithOffset(static_cast(Offset) - + TokOffset); } return Loc; } diff --git a/clang/lib/Rewrite/Rewriter.cpp b/clang/lib/Rewrite/Rewriter.cpp index ae21a10f81c35..e4dca072bad70 100644 --- a/clang/lib/Rewrite/Rewriter.cpp +++ b/clang/lib/Rewrite/Rewriter.cpp @@ -130,7 +130,7 @@ std::string Rewriter::getRewrittenText(CharSourceRange Range) const { return std::string(Start, End); } -unsigned Rewriter::getLocationOffsetAndFileID(SourceLocation Loc, +SourceLocation::UIntTy Rewriter::getLocationOffsetAndFileID(SourceLocation Loc, FileID &FID) const { assert(Loc.isValid() && "Invalid location"); FileIDAndOffset V = SourceMgr->getDecomposedLoc(Loc); diff --git a/clang/lib/Sema/MultiplexExternalSemaSource.cpp b/clang/lib/Sema/MultiplexExternalSemaSource.cpp index 9f19f13592e86..fa7cb3ec07b76 100644 --- a/clang/lib/Sema/MultiplexExternalSemaSource.cpp +++ b/clang/lib/Sema/MultiplexExternalSemaSource.cpp @@ -58,7 +58,7 @@ void MultiplexExternalSemaSource::CompleteRedeclChain(const Decl *D) { Sources[i]->CompleteRedeclChain(D); } -Selector MultiplexExternalSemaSource::GetExternalSelector(uint32_t ID) { +Selector MultiplexExternalSemaSource::GetExternalSelector(uint64_t ID) { Selector Sel; for(size_t i = 0; i < Sources.size(); ++i) { Sel = Sources[i]->GetExternalSelector(ID); diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index a34e2c9cbb003..ce9e8f81c56b4 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -5913,11 +5913,14 @@ Sema::GetNameFromUnqualifiedId(const UnqualifiedId &Name) { } case UnqualifiedIdKind::IK_OperatorFunctionId: - NameInfo.setName(Context.DeclarationNames.getCXXOperatorName( - Name.OperatorFunctionId.Operator)); - NameInfo.setCXXOperatorNameRange(SourceRange( - Name.OperatorFunctionId.SymbolLocations[0], Name.EndLocation)); - return NameInfo; + return DeclarationNameInfo( + Context.DeclarationNames.getCXXOperatorName( + Name.OperatorFunctionId.Operator), + Name.StartLocation, + DeclarationNameLoc::makeCXXOperatorNameLoc( + Context.getCXXOperatorSourceInfo( + SourceRange(Name.OperatorFunctionId.SymbolLocations[0], + Name.EndLocation)))); case UnqualifiedIdKind::IK_LiteralOperatorId: NameInfo.setName(Context.DeclarationNames.getCXXLiteralOperatorName( diff --git a/clang/lib/Sema/SemaLambda.cpp b/clang/lib/Sema/SemaLambda.cpp index 2c00616bc62d7..e172a099f5893 100644 --- a/clang/lib/Sema/SemaLambda.cpp +++ b/clang/lib/Sema/SemaLambda.cpp @@ -1004,7 +1004,8 @@ CXXMethodDecl *Sema::CreateLambdaCallOperator(SourceRange IntroducerRange, DeclarationName MethodName = Context.DeclarationNames.getCXXOperatorName(OO_Call); DeclarationNameLoc MethodNameLoc = - DeclarationNameLoc::makeCXXOperatorNameLoc(IntroducerRange.getBegin()); + DeclarationNameLoc::makeCXXOperatorNameLoc( + Context.getCXXOperatorSourceInfo(IntroducerRange.getBegin())); CXXMethodDecl *Method = CXXMethodDecl::Create( Context, Class, SourceLocation(), DeclarationNameInfo(MethodName, IntroducerRange.getBegin(), diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp index 34f512b13d6a8..e15b2722617ca 100644 --- a/clang/lib/Sema/SemaOverload.cpp +++ b/clang/lib/Sema/SemaOverload.cpp @@ -15829,8 +15829,13 @@ ExprResult Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, CXXRecordDecl *NamingClass = nullptr; // lookup ignores member operators // CHECKME: no 'operator' keyword? + // DeclarationNameInfo OpNameInfo( + // OpName, LLoc, + // DeclarationNameLoc::makeCXXOperatorNameLoc( + // Context.getCXXOperatorSourceInfo(SourceRange(LLoc, RLoc)))); DeclarationNameInfo OpNameInfo(OpName, LLoc); - OpNameInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); + OpNameInfo.setCXXOperatorNameInfo( + Context.getCXXOperatorSourceInfo(SourceRange(LLoc, RLoc))); ExprResult Fn = CreateUnresolvedLookupExpr( NamingClass, NestedNameSpecifierLoc(), OpNameInfo, UnresolvedSet<0>()); if (Fn.isInvalid()) @@ -15902,7 +15907,8 @@ ExprResult Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, // Build the actual expression node. DeclarationNameInfo OpLocInfo(OpName, LLoc); - OpLocInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); + OpLocInfo.setCXXOperatorNameInfo( + Context.getCXXOperatorSourceInfo(SourceRange(LLoc, RLoc))); ExprResult FnExpr = CreateFunctionRefExpr( *this, FnDecl, Best->FoundDecl, Base, HadMultipleCandidates, OpLocInfo.getLoc(), OpLocInfo.getInfo()); @@ -16532,7 +16538,8 @@ Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj, DeclarationNameInfo OpLocInfo( Context.DeclarationNames.getCXXOperatorName(OO_Call), LParenLoc); - OpLocInfo.setCXXOperatorNameRange(SourceRange(LParenLoc, RParenLoc)); + OpLocInfo.setCXXOperatorNameInfo( + Context.getCXXOperatorSourceInfo(SourceRange(LParenLoc, RParenLoc))); ExprResult NewFn = CreateFunctionRefExpr(*this, Method, Best->FoundDecl, Obj, HadMultipleCandidates, OpLocInfo.getLoc(), diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp index 486971818f109..f75e7df1a393b 100644 --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -6967,7 +6967,7 @@ void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) { auto &F = Diag.DiagStatesByLoc.Files[FID]; F.StateTransitions.reserve(F.StateTransitions.size() + Transitions); for (unsigned I = 0; I != Transitions; ++I) { - unsigned Offset = Record[Idx++]; + SourceLocation::UIntTy Offset = Record[Idx++]; auto *State = ReadDiagState(*FirstState, false); F.StateTransitions.push_back({State, Offset}); } @@ -9792,7 +9792,8 @@ ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) { return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo()); case DeclarationName::CXXOperatorName: - return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange()); + return DeclarationNameLoc::makeCXXOperatorNameLoc( + getASTContext().getCXXOperatorSourceInfo(readSourceRange())); case DeclarationName::CXXLiteralOperatorName: return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc( diff --git a/clang/lib/Serialization/ASTReaderStmt.cpp b/clang/lib/Serialization/ASTReaderStmt.cpp index 8945407cf666e..51115ac3796de 100644 --- a/clang/lib/Serialization/ASTReaderStmt.cpp +++ b/clang/lib/Serialization/ASTReaderStmt.cpp @@ -209,7 +209,7 @@ void ASTStmtReader::VisitAttributedStmt(AttributedStmt *S) { assert(NumAttrs == Attrs.size()); std::copy(Attrs.begin(), Attrs.end(), S->getAttrArrayPtr()); S->SubStmt = Record.readSubStmt(); - S->AttributedStmtBits.AttrLoc = readSourceLocation(); + S->AttrLoc = readSourceLocation(); } void ASTStmtReader::VisitIfStmt(IfStmt *S) { @@ -843,7 +843,8 @@ void ASTStmtReader::VisitRequiresExpr(RequiresExpr *E) { VisitExpr(E); unsigned NumLocalParameters = Record.readInt(); unsigned NumRequirements = Record.readInt(); - E->RequiresExprBits.RequiresKWLoc = Record.readSourceLocation(); + E->RequiresExprBits.RequiresKWLoc = + Record.readSourceLocation().getRawEncoding(); E->RequiresExprBits.IsSatisfied = Record.readInt(); E->Body = Record.readDeclAs(); llvm::SmallVector LocalParameters; @@ -1071,7 +1072,7 @@ void ASTStmtReader::VisitMemberExpr(MemberExpr *E) { E->MemberExprBits.HadMultipleCandidates = CurrentUnpackingBits->getNextBit(); E->MemberExprBits.NonOdrUseReason = CurrentUnpackingBits->getNextBits(/*Width=*/2); - E->MemberExprBits.OperatorLoc = Record.readSourceLocation(); + E->OperatorLoc = Record.readSourceLocation(); if (HasQualifier) new (E->getTrailingObjects()) @@ -1410,7 +1411,8 @@ void ASTStmtReader::VisitGenericSelectionExpr(GenericSelectionExpr *E) { assert(NumAssocs == E->getNumAssocs() && "Wrong NumAssocs!"); E->IsExprPredicate = Record.readInt(); E->ResultIndex = Record.readInt(); - E->GenericSelectionExprBits.GenericLoc = readSourceLocation(); + E->GenericSelectionExprBits.GenericLoc = + readSourceLocation().getRawEncoding(); E->DefaultLoc = readSourceLocation(); E->RParenLoc = readSourceLocation(); @@ -1756,7 +1758,7 @@ void ASTStmtReader::VisitCXXConstructExpr(CXXConstructExpr *E) { E->CXXConstructExprBits.ZeroInitialization = Record.readInt(); E->CXXConstructExprBits.ConstructionKind = Record.readInt(); E->CXXConstructExprBits.IsImmediateEscalating = Record.readInt(); - E->CXXConstructExprBits.Loc = readSourceLocation(); + E->Loc = readSourceLocation(); E->Constructor = readDeclAs(); E->ParenOrBraceRange = readSourceRange(); @@ -1880,7 +1882,7 @@ void ASTStmtReader::VisitCXXThisExpr(CXXThisExpr *E) { void ASTStmtReader::VisitCXXThrowExpr(CXXThrowExpr *E) { VisitExpr(E); - E->CXXThrowExprBits.ThrowLoc = readSourceLocation(); + E->CXXThrowExprBits.ThrowLoc = readSourceLocation().getRawEncoding(); E->Operand = Record.readSubExpr(); E->CXXThrowExprBits.IsThrownVariableInScope = Record.readInt(); } @@ -1889,7 +1891,7 @@ void ASTStmtReader::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { VisitExpr(E); E->Param = readDeclAs(); E->UsedContext = readDeclAs(); - E->CXXDefaultArgExprBits.Loc = readSourceLocation(); + E->CXXDefaultArgExprBits.Loc = readSourceLocation().getRawEncoding(); E->CXXDefaultArgExprBits.HasRewrittenInit = Record.readInt(); if (E->CXXDefaultArgExprBits.HasRewrittenInit) *E->getTrailingObjects() = Record.readSubExpr(); @@ -1900,7 +1902,7 @@ void ASTStmtReader::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) { E->CXXDefaultInitExprBits.HasRewrittenInit = Record.readInt(); E->Field = readDeclAs(); E->UsedContext = readDeclAs(); - E->CXXDefaultInitExprBits.Loc = readSourceLocation(); + E->CXXDefaultInitExprBits.Loc = readSourceLocation().getRawEncoding(); if (E->CXXDefaultInitExprBits.HasRewrittenInit) *E->getTrailingObjects() = Record.readSubExpr(); } @@ -1914,7 +1916,8 @@ void ASTStmtReader::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { void ASTStmtReader::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { VisitExpr(E); E->TypeInfo = readTypeSourceInfo(); - E->CXXScalarValueInitExprBits.RParenLoc = readSourceLocation(); + E->CXXScalarValueInitExprBits.RParenLoc = + readSourceLocation().getRawEncoding(); } void ASTStmtReader::VisitCXXNewExpr(CXXNewExpr *E) { @@ -1964,7 +1967,7 @@ void ASTStmtReader::VisitCXXDeleteExpr(CXXDeleteExpr *E) { E->CXXDeleteExprBits.UsualArrayDeleteWantsSize = Record.readInt(); E->OperatorDelete = readDeclAs(); E->Argument = Record.readSubExpr(); - E->CXXDeleteExprBits.Loc = readSourceLocation(); + E->CXXDeleteExprBits.Loc = readSourceLocation().getRawEncoding(); } void ASTStmtReader::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { @@ -2040,7 +2043,7 @@ void ASTStmtReader::VisitCXXDependentScopeMemberExpr( else E->Base = nullptr; - E->CXXDependentScopeMemberExprBits.OperatorLoc = readSourceLocation(); + E->CXXDependentScopeMemberExprBits.OperatorLoc = readSourceLocation().getRawEncoding(); if (HasFirstQualifierFoundInScope) *E->getTrailingObjects() = readDeclAs(); @@ -2227,7 +2230,7 @@ void ASTStmtReader::VisitSubstNonTypeTemplateParmExpr( E->Index = CurrentUnpackingBits->getNextBits(/*Width=*/12); E->PackIndex = Record.readUnsignedOrNone().toInternalRepresentation(); E->Final = CurrentUnpackingBits->getNextBit(); - E->SubstNonTypeTemplateParmExprBits.NameLoc = readSourceLocation(); + E->SubstNonTypeTemplateParmExprBits.NameLoc = readSourceLocation().getRawEncoding(); E->Replacement = Record.readSubExpr(); } @@ -2305,7 +2308,7 @@ void ASTStmtReader::VisitCXXParenListInitExpr(CXXParenListInitExpr *E) { void ASTStmtReader::VisitOpaqueValueExpr(OpaqueValueExpr *E) { VisitExpr(E); E->SourceExpr = Record.readSubExpr(); - E->OpaqueValueExprBits.Loc = readSourceLocation(); + E->OpaqueValueExprBits.Loc = readSourceLocation().getRawEncoding(); E->setIsUnique(Record.readInt()); } diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp index 06cd6c7305114..874b24b532b06 100644 --- a/clang/lib/Serialization/ASTWriter.cpp +++ b/clang/lib/Serialization/ASTWriter.cpp @@ -6652,7 +6652,7 @@ void ASTWriter::AddFileID(FileID FID, RecordDataImpl &Record) { SourceLocationEncoding::RawLocEncoding ASTWriter::getRawSourceLocationEncoding(SourceLocation Loc) { - unsigned BaseOffset = 0; + SourceLocation::UIntTy BaseOffset = 0; unsigned ModuleFileIndex = 0; // See SourceLocationEncoding.h for the encoding details. diff --git a/clang/lib/Serialization/ASTWriterDecl.cpp b/clang/lib/Serialization/ASTWriterDecl.cpp index 7f1b39c242e01..8dbc2140650f1 100644 --- a/clang/lib/Serialization/ASTWriterDecl.cpp +++ b/clang/lib/Serialization/ASTWriterDecl.cpp @@ -1021,7 +1021,7 @@ void ASTDeclWriter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) { } void ASTDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) { - static_assert(DeclContext::NumObjCContainerDeclBits == 64, + static_assert(DeclContext::NumObjCContainerDeclBits == 13, "You need to update the serializer after you change the " "ObjCContainerDeclBits"); diff --git a/clang/lib/Serialization/ASTWriterStmt.cpp b/clang/lib/Serialization/ASTWriterStmt.cpp index 87536be8c8d98..4fab00cc1c3e5 100644 --- a/clang/lib/Serialization/ASTWriterStmt.cpp +++ b/clang/lib/Serialization/ASTWriterStmt.cpp @@ -514,7 +514,7 @@ void ASTStmtWriter::VisitRequiresExpr(RequiresExpr *E) { VisitExpr(E); Record.push_back(E->getLocalParameters().size()); Record.push_back(E->getRequirements().size()); - Record.AddSourceLocation(E->RequiresExprBits.RequiresKWLoc); + Record.AddSourceLocation(E->getRequiresKWLoc()); Record.push_back(E->RequiresExprBits.IsSatisfied); Record.AddDeclRef(E->getBody()); for (ParmVarDecl *P : E->getLocalParameters()) diff --git a/clang/test/Lexer/SourceLocationsOverflow.c b/clang/test/Lexer/SourceLocationsOverflow.c deleted file mode 100644 index 26b0d204c49ff..0000000000000 --- a/clang/test/Lexer/SourceLocationsOverflow.c +++ /dev/null @@ -1,38 +0,0 @@ -// RUN: not %clang %s -S -o - 2>&1 | FileCheck %s -// CHECK: In file included from {{.*}}SourceLocationsOverflow.c -// CHECK-NEXT: inc1.h{{.*}}: fatal error: translation unit is too large for Clang to process: ran out of source locations -// CHECK-NEXT: #include "inc2.h" -// CHECK-NEXT: ^ -// CHECK-NEXT: note: 214{{.......}}B (2.15GB) in local locations, 0B (0B) in locations loaded from AST files, for a total of 214{{.......}}B (2.15GB) (99% of available space) -// CHECK-NEXT: {{.*}}inc2.h:1:1: note: file entered 214{{..}} times using 214{{.......}}B (2.15GB) of space -// CHECK-NEXT: /*................................................................................................. -// CHECK-NEXT: ^ -// CHECK-NEXT: {{.*}}inc1.h:1:1: note: file entered 15 times using 39{{....}}B (396.92kB) of space -// CHECK-NEXT: #include "inc2.h" -// CHECK-NEXT: ^ -// CHECK-NEXT: :1:1: note: file entered {{.*}} times using {{.*}}B ({{.*}}B) of space -// CHECK-NEXT: # {{.*}} -// CHECK-NEXT: ^ -// CHECK-NEXT: {{.*}}SourceLocationsOverflow.c:1:1: note: file entered 1 time using {{.*}}B ({{.*}}B) of space -// CHECK-NEXT: // RUN: not %clang %s -S -o - 2>&1 | FileCheck %s -// CHECK-NEXT: ^ -// CHECK-NEXT: 1 error generated. -#include "Inputs/inc1.h" -#include "Inputs/inc1.h" -#include "Inputs/inc1.h" -#include "Inputs/inc1.h" -#include "Inputs/inc1.h" -#include "Inputs/inc1.h" -#include "Inputs/inc1.h" -#include "Inputs/inc1.h" -#include "Inputs/inc1.h" -#include "Inputs/inc1.h" -#include "Inputs/inc1.h" -#include "Inputs/inc1.h" -#include "Inputs/inc1.h" -#include "Inputs/inc1.h" -#include "Inputs/inc1.h" -#include "Inputs/inc1.h" -#include "Inputs/inc1.h" -#include "Inputs/inc1.h" -#include "Inputs/inc1.h" diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp index 9089984fa4a54..0554e155e9380 100644 --- a/clang/tools/libclang/CIndex.cpp +++ b/clang/tools/libclang/CIndex.cpp @@ -166,16 +166,22 @@ CXSourceRange cxloc::translateSourceRange(const SourceManager &SM, Lexer::MeasureTokenLength(SM.getSpellingLoc(EndLoc), SM, LangOpts); EndLoc = EndLoc.getLocWithOffset(Length); } - - CXSourceRange Result = { - {&SM, &LangOpts}, R.getBegin().getRawEncoding(), EndLoc.getRawEncoding()}; + unsigned BeginRaw, EndRaw; + if (!R.getBegin().getRawEncoding32(BeginRaw) || + !EndLoc.getRawEncoding32(EndRaw)) + return clang_getNullRange(); // location is too big for libclang ABI + CXSourceRange Result = {{&SM, &LangOpts}, BeginRaw, EndRaw}; return Result; } CharSourceRange cxloc::translateCXRangeToCharRange(CXSourceRange R) { + if (!R.ptr_data[0]) + return CharSourceRange(); + const SourceManager &SM = + *static_cast(R.ptr_data[0]); return CharSourceRange::getCharRange( - SourceLocation::getFromRawEncoding(R.begin_int_data), - SourceLocation::getFromRawEncoding(R.end_int_data)); + SourceLocation::getFromRawEncoding32(SM, R.begin_int_data), + SourceLocation::getFromRawEncoding32(SM, R.end_int_data)); } //===----------------------------------------------------------------------===// @@ -2122,7 +2128,9 @@ class MemberRefVisit : public VisitorJob { return static_cast(data[0]); } SourceLocation getLoc() const { - return SourceLocation::getFromRawEncoding( + // this->get()->getASTContext().getSourceManager(); + return SourceLocation::getFromRawEncoding32( + this->get()->getASTContext().getSourceManager(), (SourceLocation::UIntTy)(uintptr_t)data[1]); } }; @@ -7635,8 +7643,8 @@ CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) { if (!CXXUnit) return cxstring::createEmpty(); - SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]); - FileIDAndOffset LocInfo = + SourceLocation Loc = SourceLocation::getFromRawEncoding32(CXXUnit->getSourceManager(), CXTok.int_data[1]); + auto LocInfo = CXXUnit->getSourceManager().getDecomposedSpellingLoc(Loc); bool Invalid = false; StringRef Buffer = @@ -7659,7 +7667,7 @@ CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) { return cxloc::translateSourceLocation( CXXUnit->getASTContext(), - SourceLocation::getFromRawEncoding(CXTok.int_data[1])); + SourceLocation::getFromRawEncoding32(CXXUnit->getSourceManager(), CXTok.int_data[1])); } CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) { @@ -7674,10 +7682,10 @@ CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) { return cxloc::translateSourceRange( CXXUnit->getASTContext(), - SourceLocation::getFromRawEncoding(CXTok.int_data[1])); + SourceLocation::getFromRawEncoding32(CXXUnit->getSourceManager(), CXTok.int_data[1])); } -static void getTokens(ASTUnit *CXXUnit, SourceRange Range, +static bool getTokens(ASTUnit *CXXUnit, SourceRange Range, SmallVectorImpl &CXTokens) { SourceManager &SourceMgr = CXXUnit->getSourceManager(); FileIDAndOffset BeginLocInfo = @@ -7687,13 +7695,13 @@ static void getTokens(ASTUnit *CXXUnit, SourceRange Range, // Cannot tokenize across files. if (BeginLocInfo.first != EndLocInfo.first) - return; + return false; // Create a lexer bool Invalid = false; StringRef Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); if (Invalid) - return; + return false; Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), CXXUnit->getASTContext().getLangOpts(), Buffer.begin(), @@ -7714,7 +7722,12 @@ static void getTokens(ASTUnit *CXXUnit, SourceRange Range, CXToken CXTok; // - Common fields - CXTok.int_data[1] = Tok.getLocation().getRawEncoding(); + // CXTok.int_data[1] = Tok.getLocation().getRawEncoding(); + uint32_t TokLocRaw; + if (!Tok.getLocation().getRawEncoding32(TokLocRaw)) + return false; // location is too big for libclang ABI + CXTok.int_data[1] = TokLocRaw; + CXTok.int_data[2] = Tok.getLength(); CXTok.int_data[3] = 0; @@ -7743,6 +7756,8 @@ static void getTokens(ASTUnit *CXXUnit, SourceRange Range, CXTokens.push_back(CXTok); previousWasAt = Tok.is(tok::at); } while (Lex.getBufferLocation() < EffectiveBufferEnd); + + return true; } CXToken *clang_getToken(CXTranslationUnit TU, CXSourceLocation Location) { @@ -7769,7 +7784,8 @@ CXToken *clang_getToken(CXTranslationUnit TU, CXSourceLocation Location) { SM.getComposedLoc(DecomposedEnd.first, DecomposedEnd.second); SmallVector CXTokens; - getTokens(CXXUnit, SourceRange(Begin, End), CXTokens); + if (!getTokens(CXXUnit, SourceRange(Begin, End), CXTokens)) + return nullptr; if (CXTokens.empty()) return nullptr; @@ -7806,7 +7822,8 @@ void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range, CXToken **Tokens, return; SmallVector CXTokens; - getTokens(CXXUnit, R, CXTokens); + if (!getTokens(CXXUnit, R, CXTokens)) + return; if (CXTokens.empty()) return; @@ -7870,13 +7887,15 @@ class AnnotateTokensWorker { unsigned NextToken() const { return TokIdx; } void AdvanceToken() { ++TokIdx; } SourceLocation GetTokenLoc(unsigned tokI) { - return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[1]); + return SourceLocation::getFromRawEncoding32(SrcMgr, + getTok(tokI).int_data[1]); } bool isFunctionMacroToken(unsigned tokI) const { return getTok(tokI).int_data[3] != 0; } SourceLocation getFunctionMacroTokenLoc(unsigned tokI) const { - return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[3]); + return SourceLocation::getFromRawEncoding32(SrcMgr, + getTok(tokI).int_data[3]); } void annotateAndAdvanceTokens(CXCursor, RangeComparisonResult, SourceRange); @@ -8371,13 +8390,16 @@ class MarkMacroArgTokensVisitor { } SourceLocation getTokenLoc(unsigned tokI) { - return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[1]); + return SourceLocation::getFromRawEncoding32(SM, getTok(tokI).int_data[1]); } void setFunctionMacroTokenLoc(unsigned tokI, SourceLocation loc) { // The third field is reserved and currently not used. Use it here // to mark macro arg expanded tokens with their expanded locations. - getTok(tokI).int_data[3] = loc.getRawEncoding(); + uint32_t Raw; + if (!loc.getRawEncoding32(Raw)) + Raw = 0; // invalid source location + getTok(tokI).int_data[3] = Raw; } }; @@ -8437,8 +8459,8 @@ static void annotatePreprocessorTokens(CXTranslationUnit TU, if (lexNext(Lex, Tok, NextIdx, NumTokens)) break; unsigned TokIdx = NextIdx - 1; - assert(Tok.getLocation() == - SourceLocation::getFromRawEncoding(Tokens[TokIdx].int_data[1])); + assert(Tok.getLocation() == SourceLocation::getFromRawEncoding32( + SourceMgr, Tokens[TokIdx].int_data[1])); reprocess: if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { @@ -8488,8 +8510,8 @@ static void annotatePreprocessorTokens(CXTranslationUnit TU, unsigned LastIdx = finished ? NextIdx - 1 : NextIdx - 2; assert(TokIdx <= LastIdx); - SourceLocation EndLoc = - SourceLocation::getFromRawEncoding(Tokens[LastIdx].int_data[1]); + SourceLocation EndLoc = SourceLocation::getFromRawEncoding32( + SourceMgr, Tokens[LastIdx].int_data[1]); CXCursor Cursor = MakePreprocessingDirectiveCursor(SourceRange(BeginLoc, EndLoc), TU); diff --git a/clang/tools/libclang/CXIndexDataConsumer.cpp b/clang/tools/libclang/CXIndexDataConsumer.cpp index f0d92e8c40124..938dd85737a35 100644 --- a/clang/tools/libclang/CXIndexDataConsumer.cpp +++ b/clang/tools/libclang/CXIndexDataConsumer.cpp @@ -988,8 +988,8 @@ bool CXIndexDataConsumer::handleCXXRecordDecl(const CXXRecordDecl *RD, const CXIdxBaseClassInfo *baseInfo = BaseList.getBases()[i]; if (baseInfo->base) { const NamedDecl *BaseD = BaseList.BaseEntities[i].Dcl; - SourceLocation - Loc = SourceLocation::getFromRawEncoding(baseInfo->loc.int_data); + SourceLocation Loc = SourceLocation::getFromRawEncoding32( + Ctx->getSourceManager(), baseInfo->loc.int_data); markEntityOccurrenceInFile(BaseD, Loc); } } @@ -1077,9 +1077,11 @@ CXIdxLoc CXIndexDataConsumer::getIndexLoc(SourceLocation Loc) const { CXIdxLoc idxLoc = { {nullptr, nullptr}, 0 }; if (Loc.isInvalid()) return idxLoc; - + uint32_t LocRaw; + if (!Loc.getRawEncoding32(LocRaw)) + return idxLoc; idxLoc.ptr_data[0] = const_cast(this); - idxLoc.int_data = Loc.getRawEncoding(); + idxLoc.int_data = LocRaw; return idxLoc; } diff --git a/clang/tools/libclang/CXSourceLocation.cpp b/clang/tools/libclang/CXSourceLocation.cpp index 4c16e5de4498a..6ea42f795310f 100644 --- a/clang/tools/libclang/CXSourceLocation.cpp +++ b/clang/tools/libclang/CXSourceLocation.cpp @@ -211,24 +211,27 @@ static void createNullLocation(CXString *filename, unsigned *line, } int clang_Location_isInSystemHeader(CXSourceLocation location) { + if (!location.ptr_data[0]) + return 0; + const SourceManager &SM = + *static_cast(location.ptr_data[0]); const SourceLocation Loc = - SourceLocation::getFromRawEncoding(location.int_data); + SourceLocation::getFromRawEncoding32(SM, location.int_data); if (Loc.isInvalid()) return 0; - const SourceManager &SM = - *static_cast(location.ptr_data[0]); return SM.isInSystemHeader(Loc); } int clang_Location_isFromMainFile(CXSourceLocation location) { + if (!location.ptr_data[0]) + return 0; + const SourceManager &SM = + *static_cast(location.ptr_data[0]); const SourceLocation Loc = - SourceLocation::getFromRawEncoding(location.int_data); + SourceLocation::getFromRawEncoding32(SM, location.int_data); if (Loc.isInvalid()) return 0; - - const SourceManager &SM = - *static_cast(location.ptr_data[0]); return SM.isWrittenInMainFile(Loc); } @@ -241,16 +244,21 @@ void clang_getExpansionLocation(CXSourceLocation location, CXLoadedDiagnostic::decodeLocation(location, file, line, column, offset); return; } + if (!location.ptr_data[0]) { + createNullLocation(file, line, column, offset); + return; + } - SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); + const SourceManager &SM = + *static_cast(location.ptr_data[0]); + SourceLocation Loc = + SourceLocation::getFromRawEncoding32(SM, location.int_data); - if (!location.ptr_data[0] || Loc.isInvalid()) { + if (Loc.isInvalid()) { createNullLocation(file, line, column, offset); return; } - const SourceManager &SM = - *static_cast(location.ptr_data[0]); SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc); // Check that the FileID is invalid on the expansion location. @@ -283,16 +291,21 @@ void clang_getPresumedLocation(CXSourceLocation location, createNullLocation(filename, line, column); return; } - - SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); - - if (!location.ptr_data[0] || Loc.isInvalid()) { + if (!location.ptr_data[0]) { createNullLocation(filename, line, column); return; } const SourceManager &SM = *static_cast(location.ptr_data[0]); + SourceLocation Loc = + SourceLocation::getFromRawEncoding32(SM, location.int_data); + + if (Loc.isInvalid()) { + createNullLocation(filename, line, column); + return; + } + PresumedLoc PreLoc = SM.getPresumedLoc(Loc); if (PreLoc.isInvalid()) { createNullLocation(filename, line, column); @@ -323,14 +336,16 @@ void clang_getSpellingLocation(CXSourceLocation location, column, offset); return; } - - SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); - - if (!location.ptr_data[0] || Loc.isInvalid()) + if (!location.ptr_data[0]) return createNullLocation(file, line, column, offset); - const SourceManager &SM = - *static_cast(location.ptr_data[0]); + *static_cast(location.ptr_data[0]); + SourceLocation Loc = + SourceLocation::getFromRawEncoding32(SM, location.int_data); + + if (Loc.isInvalid()) + return createNullLocation(file, line, column, offset); + SourceLocation SpellLoc = SM.getSpellingLoc(Loc); FileIDAndOffset LocInfo = SM.getDecomposedLoc(SpellLoc); FileID FID = LocInfo.first; @@ -360,13 +375,17 @@ void clang_getFileLocation(CXSourceLocation location, return; } - SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); - - if (!location.ptr_data[0] || Loc.isInvalid()) + if (!location.ptr_data[0]) return createNullLocation(file, line, column, offset); const SourceManager &SM = - *static_cast(location.ptr_data[0]); + *static_cast(location.ptr_data[0]); + SourceLocation Loc = + SourceLocation::getFromRawEncoding32(SM, location.int_data); + + if (Loc.isInvalid()) + return createNullLocation(file, line, column, offset); + SourceLocation FileLoc = SM.getFileLoc(Loc); FileIDAndOffset LocInfo = SM.getDecomposedLoc(FileLoc); FileID FID = LocInfo.first; diff --git a/clang/tools/libclang/CXSourceLocation.h b/clang/tools/libclang/CXSourceLocation.h index c86f6850375bb..bc36db3571b63 100644 --- a/clang/tools/libclang/CXSourceLocation.h +++ b/clang/tools/libclang/CXSourceLocation.h @@ -30,9 +30,15 @@ translateSourceLocation(const SourceManager &SM, const LangOptions &LangOpts, SourceLocation Loc) { if (Loc.isInvalid()) return clang_getNullLocation(); + uint32_t LocRaw; + if (!Loc.getRawEncoding32(LocRaw)) + return clang_getNullLocation(); // location is too big for libclang ABI - CXSourceLocation Result = { { &SM, &LangOpts, }, - Loc.getRawEncoding() }; + CXSourceLocation Result = {{ + &SM, + &LangOpts, + }, + LocRaw}; return Result; } @@ -63,12 +69,21 @@ static inline CXSourceRange translateSourceRange(ASTContext &Context, } static inline SourceLocation translateSourceLocation(CXSourceLocation L) { - return SourceLocation::getFromRawEncoding(L.int_data); + if (!L.ptr_data[0]) { + return SourceLocation(); + } + const SourceManager &SM = + *static_cast(L.ptr_data[0]); + return SourceLocation::getFromRawEncoding32(SM, L.int_data); } static inline SourceRange translateCXSourceRange(CXSourceRange R) { - return SourceRange(SourceLocation::getFromRawEncoding(R.begin_int_data), - SourceLocation::getFromRawEncoding(R.end_int_data)); + if (!R.ptr_data[0]) { + return SourceRange(); + } + const SourceManager &SM = *static_cast(R.ptr_data[0]); + return SourceRange(SourceLocation::getFromRawEncoding32(SM, R.begin_int_data), + SourceLocation::getFromRawEncoding32(SM, R.end_int_data)); } /// Translates CXSourceRange to CharSourceRange. diff --git a/clang/tools/libclang/Indexing.cpp b/clang/tools/libclang/Indexing.cpp index 3cd49bf90235b..da3f3f973a675 100644 --- a/clang/tools/libclang/Indexing.cpp +++ b/clang/tools/libclang/Indexing.cpp @@ -979,23 +979,30 @@ void clang_indexLoc_getFileLocation(CXIdxLoc location, if (file) *file = nullptr; if (line) *line = 0; if (column) *column = 0; - if (offset) *offset = 0; - - SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); - if (!location.ptr_data[0] || Loc.isInvalid()) + if (offset) + *offset = 0; + if (!location.ptr_data[0]) return; - CXIndexDataConsumer &DataConsumer = - *static_cast(location.ptr_data[0]); + *static_cast(location.ptr_data[0]); + SourceLocation Loc = SourceLocation::getFromRawEncoding32( + DataConsumer.getASTContext().getSourceManager(), location.int_data); + if (Loc.isInvalid()) + return; + DataConsumer.translateLoc(Loc, indexFile, file, line, column, offset); } CXSourceLocation clang_indexLoc_getCXSourceLocation(CXIdxLoc location) { - SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); - if (!location.ptr_data[0] || Loc.isInvalid()) - return clang_getNullLocation(); + if (!location.ptr_data[0]) + return clang_getNullLocation(); CXIndexDataConsumer &DataConsumer = - *static_cast(location.ptr_data[0]); + *static_cast(location.ptr_data[0]); + const auto &SM = DataConsumer.getASTContext().getSourceManager(); + SourceLocation Loc = + SourceLocation::getFromRawEncoding32(SM, location.int_data); + if (Loc.isInvalid()) + return clang_getNullLocation(); return cxloc::translateSourceLocation(DataConsumer.getASTContext(), Loc); } diff --git a/clang/unittests/Lex/PPMemoryAllocationsTest.cpp b/clang/unittests/Lex/PPMemoryAllocationsTest.cpp index 4d83003e28b36..7b36d5f3326e9 100644 --- a/clang/unittests/Lex/PPMemoryAllocationsTest.cpp +++ b/clang/unittests/Lex/PPMemoryAllocationsTest.cpp @@ -87,7 +87,7 @@ TEST_F(PPMemoryAllocationsTest, PPMacroDefinesAllocations) { // Assume a reasonable upper bound based on that number that we don't want // to exceed when storing information about a macro #define with 1 or 3 // tokens. - EXPECT_LT(BytesPerDefine, 130.0f); + EXPECT_LT(BytesPerDefine, 150.0f); } } // anonymous namespace diff --git a/clang/unittests/Serialization/SourceLocationEncodingTest.cpp b/clang/unittests/Serialization/SourceLocationEncodingTest.cpp index 18fedd4de3973..63aec9a94981b 100644 --- a/clang/unittests/Serialization/SourceLocationEncodingTest.cpp +++ b/clang/unittests/Serialization/SourceLocationEncodingTest.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "clang/Serialization/SourceLocationEncoding.h" +#include "llvm/Support/MathExtras.h" #include "gtest/gtest.h" #include @@ -31,11 +32,13 @@ void roundTrip(SourceLocation::UIntTy Loc, SourceLocation::UIntTy DecodedEncoded = SourceLocationEncoding::decode(ActualEncoded).first.getRawEncoding(); ASSERT_EQ(DecodedEncoded, Loc) << "Decoding " << ActualEncoded; -} +} constexpr SourceLocation::UIntTy MacroBit = - 1 << (sizeof(SourceLocation::UIntTy) * CHAR_BIT - 1); -constexpr SourceLocation::UIntTy Big = MacroBit >> 1; + 1ull << (SourceLocation::Bits - 1); +constexpr SourceLocation::UIntTy Big = 1ull << (SourceLocation::Bits - 2); +constexpr SourceLocation::UIntTy Biggest = + llvm::maskTrailingOnes(SourceLocation::Bits - 1); TEST(SourceLocationEncoding, Individual) { roundTrip(1, 2); @@ -46,6 +49,8 @@ TEST(SourceLocationEncoding, Individual) { roundTrip(Big + 1); roundTrip(MacroBit | Big); roundTrip(MacroBit | (Big + 1)); + roundTrip(Biggest); + roundTrip(MacroBit | Biggest); } } // namespace