From 7b97aef99774bcb60a2f0ef6330923b61c501766 Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Wed, 7 Nov 2012 00:12:38 +0000 Subject: [PATCH] PR13552: Fix the end location of a CXXNewExpr. Spent longer than reasonable looking for a nice way to test this & decided to give up for now. Open to suggestions/requests. Richard Smith suggested adding something to ASTMatchers but it wasn't readily apparent how to test this with that. llvm-svn: 167507 --- clang/include/clang/AST/ExprCXX.h | 12 ++++++------ clang/include/clang/Sema/Sema.h | 2 +- clang/lib/AST/ExprCXX.cpp | 16 ++-------------- clang/lib/Sema/SemaExprCXX.cpp | 7 ++++--- clang/lib/Serialization/ASTReaderStmt.cpp | 2 +- clang/lib/Serialization/ASTWriterStmt.cpp | 2 +- 6 files changed, 15 insertions(+), 26 deletions(-) diff --git a/clang/include/clang/AST/ExprCXX.h b/clang/include/clang/AST/ExprCXX.h index 96603eb..9c759db 100644 --- a/clang/include/clang/AST/ExprCXX.h +++ b/clang/include/clang/AST/ExprCXX.h @@ -1463,8 +1463,8 @@ class CXXNewExpr : public Expr { /// the source range covering the parenthesized type-id. SourceRange TypeIdParens; - /// \brief Location of the first token. - SourceLocation StartLoc; + /// \brief Range of the entire new expression. + SourceRange Range; /// \brief Source-range of a paren-delimited initializer. SourceRange DirectInitRange; @@ -1498,7 +1498,7 @@ public: SourceRange typeIdParens, Expr *arraySize, InitializationStyle initializationStyle, Expr *initializer, QualType ty, TypeSourceInfo *AllocatedTypeInfo, - SourceLocation startLoc, SourceRange directInitRange); + SourceRange Range, SourceRange directInitRange); explicit CXXNewExpr(EmptyShell Shell) : Expr(CXXNewExprClass, Shell), SubExprs(0) { } @@ -1613,13 +1613,13 @@ public: return SubExprs + Array + hasInitializer() + getNumPlacementArgs(); } - SourceLocation getStartLoc() const { return StartLoc; } - SourceLocation getEndLoc() const; + SourceLocation getStartLoc() const { return Range.getBegin(); } + SourceLocation getEndLoc() const { return Range.getEnd(); } SourceRange getDirectInitRange() const { return DirectInitRange; } SourceRange getSourceRange() const LLVM_READONLY { - return SourceRange(getStartLoc(), getEndLoc()); + return Range; } static bool classof(const Stmt *T) { diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index 4183dcf..9b572d8 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -3762,7 +3762,7 @@ public: SourceLocation PlacementRParen, SourceRange TypeIdParens, Declarator &D, Expr *Initializer); - ExprResult BuildCXXNew(SourceLocation StartLoc, bool UseGlobal, + ExprResult BuildCXXNew(SourceRange Range, bool UseGlobal, SourceLocation PlacementLParen, MultiExprArg PlacementArgs, SourceLocation PlacementRParen, diff --git a/clang/lib/AST/ExprCXX.cpp b/clang/lib/AST/ExprCXX.cpp index 9758b60..f60f195 100644 --- a/clang/lib/AST/ExprCXX.cpp +++ b/clang/lib/AST/ExprCXX.cpp @@ -88,14 +88,14 @@ CXXNewExpr::CXXNewExpr(ASTContext &C, bool globalNew, FunctionDecl *operatorNew, InitializationStyle initializationStyle, Expr *initializer, QualType ty, TypeSourceInfo *allocatedTypeInfo, - SourceLocation startLoc, SourceRange directInitRange) + SourceRange Range, SourceRange directInitRange) : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary, ty->isDependentType(), ty->isDependentType(), ty->isInstantiationDependentType(), ty->containsUnexpandedParameterPack()), SubExprs(0), OperatorNew(operatorNew), OperatorDelete(operatorDelete), AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens), - StartLoc(startLoc), DirectInitRange(directInitRange), + Range(Range), DirectInitRange(directInitRange), GlobalNew(globalNew), UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) { assert((initializer != 0 || initializationStyle == NoInit) && "Only NoInit can have no initializer."); @@ -147,18 +147,6 @@ bool CXXNewExpr::shouldNullCheckAllocation(ASTContext &Ctx) const { castAs()->isNothrow(Ctx); } -SourceLocation CXXNewExpr::getEndLoc() const { - switch (getInitializationStyle()) { - case NoInit: - return AllocatedTypeInfo->getTypeLoc().getEndLoc(); - case CallInit: - return DirectInitRange.getEnd(); - case ListInit: - return getInitializer()->getSourceRange().getEnd(); - } - llvm_unreachable("bogus initialization style"); -} - // CXXDeleteExpr QualType CXXDeleteExpr::getDestroyedType() const { const Expr *Arg = getArgument(); diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp index bed6cb0..0919bc5 100644 --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -987,7 +987,7 @@ Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, if (ParenListExpr *List = dyn_cast_or_null(Initializer)) DirectInitRange = List->getSourceRange(); - return BuildCXXNew(StartLoc, UseGlobal, + return BuildCXXNew(SourceRange(StartLoc, D.getLocEnd()), UseGlobal, PlacementLParen, PlacementArgs, PlacementRParen, @@ -1020,7 +1020,7 @@ static bool isLegalArrayNewInitializer(CXXNewExpr::InitializationStyle Style, } ExprResult -Sema::BuildCXXNew(SourceLocation StartLoc, bool UseGlobal, +Sema::BuildCXXNew(SourceRange Range, bool UseGlobal, SourceLocation PlacementLParen, MultiExprArg PlacementArgs, SourceLocation PlacementRParen, @@ -1032,6 +1032,7 @@ Sema::BuildCXXNew(SourceLocation StartLoc, bool UseGlobal, Expr *Initializer, bool TypeMayContainAuto) { SourceRange TypeRange = AllocTypeInfo->getTypeLoc().getSourceRange(); + SourceLocation StartLoc = Range.getBegin(); CXXNewExpr::InitializationStyle initStyle; if (DirectInitRange.isValid()) { @@ -1407,7 +1408,7 @@ Sema::BuildCXXNew(SourceLocation StartLoc, bool UseGlobal, TypeIdParens, ArraySize, initStyle, Initializer, ResultType, AllocTypeInfo, - StartLoc, DirectInitRange)); + Range, DirectInitRange)); } /// \brief Checks that a type is suitable as the allocated type diff --git a/clang/lib/Serialization/ASTReaderStmt.cpp b/clang/lib/Serialization/ASTReaderStmt.cpp index 52b4435..4c3e315 100644 --- a/clang/lib/Serialization/ASTReaderStmt.cpp +++ b/clang/lib/Serialization/ASTReaderStmt.cpp @@ -1243,7 +1243,7 @@ void ASTStmtReader::VisitCXXNewExpr(CXXNewExpr *E) { E->setOperatorDelete(ReadDeclAs(Record, Idx)); E->AllocatedTypeInfo = GetTypeSourceInfo(Record, Idx); E->TypeIdParens = ReadSourceRange(Record, Idx); - E->StartLoc = ReadSourceLocation(Record, Idx); + E->Range = ReadSourceRange(Record, Idx); E->DirectInitRange = ReadSourceRange(Record, Idx); E->AllocateArgsArray(Reader.getContext(), isArray, NumPlacementArgs, diff --git a/clang/lib/Serialization/ASTWriterStmt.cpp b/clang/lib/Serialization/ASTWriterStmt.cpp index 121d4d6..b1ee84b 100644 --- a/clang/lib/Serialization/ASTWriterStmt.cpp +++ b/clang/lib/Serialization/ASTWriterStmt.cpp @@ -1236,7 +1236,7 @@ void ASTStmtWriter::VisitCXXNewExpr(CXXNewExpr *E) { Writer.AddDeclRef(E->getOperatorDelete(), Record); Writer.AddTypeSourceInfo(E->getAllocatedTypeSourceInfo(), Record); Writer.AddSourceRange(E->getTypeIdParens(), Record); - Writer.AddSourceLocation(E->getStartLoc(), Record); + Writer.AddSourceRange(E->getSourceRange(), Record); Writer.AddSourceRange(E->getDirectInitRange(), Record); for (CXXNewExpr::arg_iterator I = E->raw_arg_begin(), e = E->raw_arg_end(); I != e; ++I) -- 2.7.4