Index: llvm/trunk/include/llvm/MC/MCExpr.h =================================================================== --- llvm/trunk/include/llvm/MC/MCExpr.h +++ llvm/trunk/include/llvm/MC/MCExpr.h @@ -13,6 +13,7 @@ #include "llvm/ADT/DenseMap.h" #include "llvm/Support/Casting.h" #include "llvm/Support/DataTypes.h" +#include "llvm/Support/SMLoc.h" namespace llvm { class MCAsmInfo; @@ -43,6 +44,7 @@ private: ExprKind Kind; + SMLoc Loc; MCExpr(const MCExpr&) = delete; void operator=(const MCExpr&) = delete; @@ -56,7 +58,7 @@ const SectionAddrMap *Addrs, bool InSet) const; protected: - explicit MCExpr(ExprKind Kind) : Kind(Kind) {} + explicit MCExpr(ExprKind Kind, SMLoc Loc) : Kind(Kind), Loc(Loc) {} bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm, const MCAsmLayout *Layout, @@ -68,6 +70,7 @@ /// @{ ExprKind getKind() const { return Kind; } + SMLoc getLoc() const { return Loc; } /// @} /// \name Utility Methods @@ -132,7 +135,7 @@ int64_t Value; explicit MCConstantExpr(int64_t Value) - : MCExpr(MCExpr::Constant), Value(Value) {} + : MCExpr(MCExpr::Constant, SMLoc()), Value(Value) {} public: /// \name Construction @@ -289,7 +292,7 @@ const MCSymbol *Symbol; explicit MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind, - const MCAsmInfo *MAI); + const MCAsmInfo *MAI, SMLoc Loc = SMLoc()); public: /// \name Construction @@ -300,7 +303,7 @@ } static const MCSymbolRefExpr *create(const MCSymbol *Symbol, VariantKind Kind, - MCContext &Ctx); + MCContext &Ctx, SMLoc Loc = SMLoc()); static const MCSymbolRefExpr *create(StringRef Name, VariantKind Kind, MCContext &Ctx); @@ -346,7 +349,7 @@ const MCExpr *Expr; MCUnaryExpr(Opcode Op, const MCExpr *Expr) - : MCExpr(MCExpr::Unary), Op(Op), Expr(Expr) {} + : MCExpr(MCExpr::Unary, SMLoc()), Op(Op), Expr(Expr) {} public: /// \name Construction @@ -417,15 +420,17 @@ Opcode Op; const MCExpr *LHS, *RHS; - MCBinaryExpr(Opcode Op, const MCExpr *LHS, const MCExpr *RHS) - : MCExpr(MCExpr::Binary), Op(Op), LHS(LHS), RHS(RHS) {} + MCBinaryExpr(Opcode Op, const MCExpr *LHS, const MCExpr *RHS, + SMLoc Loc = SMLoc()) + : MCExpr(MCExpr::Binary, Loc), Op(Op), LHS(LHS), RHS(RHS) {} public: /// \name Construction /// @{ static const MCBinaryExpr *create(Opcode Op, const MCExpr *LHS, - const MCExpr *RHS, MCContext &Ctx); + const MCExpr *RHS, MCContext &Ctx, + SMLoc Loc = SMLoc()); static const MCBinaryExpr *createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx) { return create(Add, LHS, RHS, Ctx); @@ -531,7 +536,7 @@ class MCTargetExpr : public MCExpr { virtual void anchor(); protected: - MCTargetExpr() : MCExpr(Target) {} + MCTargetExpr() : MCExpr(Target, SMLoc()) {} virtual ~MCTargetExpr() {} public: virtual void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const = 0; Index: llvm/trunk/lib/MC/MCExpr.cpp =================================================================== --- llvm/trunk/lib/MC/MCExpr.cpp +++ llvm/trunk/lib/MC/MCExpr.cpp @@ -137,8 +137,9 @@ /* *** */ const MCBinaryExpr *MCBinaryExpr::create(Opcode Opc, const MCExpr *LHS, - const MCExpr *RHS, MCContext &Ctx) { - return new (Ctx) MCBinaryExpr(Opc, LHS, RHS); + const MCExpr *RHS, MCContext &Ctx, + SMLoc Loc) { + return new (Ctx) MCBinaryExpr(Opc, LHS, RHS, Loc); } const MCUnaryExpr *MCUnaryExpr::create(Opcode Opc, const MCExpr *Expr, @@ -153,8 +154,8 @@ /* *** */ MCSymbolRefExpr::MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind, - const MCAsmInfo *MAI) - : MCExpr(MCExpr::SymbolRef), Kind(Kind), + const MCAsmInfo *MAI, SMLoc Loc) + : MCExpr(MCExpr::SymbolRef, Loc), Kind(Kind), UseParensForSymbolVariant(MAI->useParensForSymbolVariant()), HasSubsectionsViaSymbols(MAI->hasSubsectionsViaSymbols()), Symbol(Symbol) { @@ -163,8 +164,8 @@ const MCSymbolRefExpr *MCSymbolRefExpr::create(const MCSymbol *Sym, VariantKind Kind, - MCContext &Ctx) { - return new (Ctx) MCSymbolRefExpr(Sym, Kind, Ctx.getAsmInfo()); + MCContext &Ctx, SMLoc Loc) { + return new (Ctx) MCSymbolRefExpr(Sym, Kind, Ctx.getAsmInfo(), Loc); } const MCSymbolRefExpr *MCSymbolRefExpr::create(StringRef Name, VariantKind Kind, Index: llvm/trunk/lib/MC/MCFragment.cpp =================================================================== --- llvm/trunk/lib/MC/MCFragment.cpp +++ llvm/trunk/lib/MC/MCFragment.cpp @@ -145,14 +145,14 @@ MCValue Value; if (!Expr->evaluateAsValue(Value, *this)) { Assembler.getContext().reportError( - SMLoc(), "expression could not be evaluated"); + Expr->getLoc(), "expression could not be evaluated"); return nullptr; } const MCSymbolRefExpr *RefB = Value.getSymB(); if (RefB) { Assembler.getContext().reportError( - SMLoc(), Twine("symbol '") + RefB->getSymbol().getName() + + Expr->getLoc(), Twine("symbol '") + RefB->getSymbol().getName() + "' could not be evaluated in a subtraction expression"); return nullptr; } @@ -164,8 +164,7 @@ const MCSymbol &ASym = A->getSymbol(); const MCAssembler &Asm = getAssembler(); if (ASym.isCommon()) { - // FIXME: we should probably add a SMLoc to MCExpr. - Asm.getContext().reportError(SMLoc(), + Asm.getContext().reportError(Expr->getLoc(), "Common symbol '" + ASym.getName() + "' cannot be used in assignment expr"); return nullptr; Index: llvm/trunk/lib/MC/MCParser/AsmParser.cpp =================================================================== --- llvm/trunk/lib/MC/MCParser/AsmParser.cpp +++ llvm/trunk/lib/MC/MCParser/AsmParser.cpp @@ -1005,7 +1005,7 @@ } // Otherwise create a symbol ref. - Res = MCSymbolRefExpr::create(Sym, Variant, getContext()); + Res = MCSymbolRefExpr::create(Sym, Variant, getContext(), FirstTokenLoc); return false; } case AsmToken::BigNum: @@ -1436,6 +1436,7 @@ /// Res contains the LHS of the expression on input. bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc) { + SMLoc StartLoc = Lexer.getLoc(); while (true) { MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add; unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind); @@ -1460,7 +1461,7 @@ return true; // Merge LHS and RHS according to operator. - Res = MCBinaryExpr::create(Kind, Res, RHS, getContext()); + Res = MCBinaryExpr::create(Kind, Res, RHS, getContext(), StartLoc); } } Index: llvm/trunk/test/MC/AArch64/error-location-post-layout.s =================================================================== --- llvm/trunk/test/MC/AArch64/error-location-post-layout.s +++ llvm/trunk/test/MC/AArch64/error-location-post-layout.s @@ -1,14 +1,11 @@ // RUN: not llvm-mc -triple aarch64--none-eabi -filetype obj < %s -o /dev/null 2>&1 | FileCheck %s -// Note: These errors are not always emitted in the order in which the relevant -// source appears, this file is carefully ordered so that that is the case. - -// CHECK: :0: error: expression could not be evaluated .set v1, -undef +// CHECK: :0: error: expression could not be evaluated .comm common, 4 -// CHECK: :0: error: Common symbol 'common' cannot be used in assignment expr .set v3, common +// CHECK: 7:12: error: Common symbol 'common' cannot be used in assignment expr -// CHECK: :0: error: symbol 'undef' could not be evaluated in a subtraction expression .set v2, a-undef +// CHECK: 10:13: error: symbol 'undef' could not be evaluated in a subtraction expression Index: llvm/trunk/test/MC/ARM/error-location-post-layout.s =================================================================== --- llvm/trunk/test/MC/ARM/error-location-post-layout.s +++ llvm/trunk/test/MC/ARM/error-location-post-layout.s @@ -1,14 +1,14 @@ @ RUN: not llvm-mc -triple armv7a--none-eabi -filetype obj < %s -o /dev/null 2>&1 | FileCheck %s -@ Note: These errors are not always emitted in the order in which the relevant -@ source appears, this file is carefully ordered so that that is the case. - -@ CHECK: :0: error: expression could not be evaluated .set v1, -undef +@ CHECK: :0: error: expression could not be evaluated .comm common, 4 -@ CHECK: :0: error: Common symbol 'common' cannot be used in assignment expr .set v3, common +@ CHECK: 7:12: error: Common symbol 'common' cannot be used in assignment expr -@ CHECK: :0: error: symbol 'undef' could not be evaluated in a subtraction expression .set v2, a-undef +@ CHECK-DAG: 10:13: error: symbol 'undef' could not be evaluated in a subtraction expression + + .equ STACK_START, (a + undef) +@ CHECK-DAG: 13:24: error: expression could not be evaluated