Verifier: Check fields of MDVariable subclasses

Check fields from `MDLocalVariable` and `MDGlobalVariable` and change
the accessors to downcast to the right types.  `getType()` still returns
`Metadata*` since it could be an `MDString`-based reference.

Since local variables require non-null scopes, I also updated `LLParser`
to require a `scope:` field.

A number of testcases had grown bitrot and started failing with this
patch; I committed them separately in r233349.  If I just broke your
out-of-tree testcases, you're probably hitting similar problems (so have
a look there).

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@233389 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Duncan P. N. Exon Smith 2015-03-27 17:29:58 +00:00
parent 2ee615297c
commit a9902daa5c
16 changed files with 205 additions and 107 deletions

View File

@ -1315,12 +1315,15 @@ protected:
public: public:
unsigned getLine() const { return Line; } unsigned getLine() const { return Line; }
Metadata *getScope() const { return getOperand(0); } MDScope *getScope() const { return cast_or_null<MDScope>(getRawScope()); }
StringRef getName() const { return getStringOperand(1); } StringRef getName() const { return getStringOperand(1); }
Metadata *getFile() const { return getOperand(2); } MDFile *getFile() const { return cast_or_null<MDFile>(getRawFile()); }
Metadata *getType() const { return getOperand(3); } Metadata *getType() const { return getRawType(); }
Metadata *getRawScope() const { return getOperand(0); }
MDString *getRawName() const { return getOperandAs<MDString>(1); } MDString *getRawName() const { return getOperandAs<MDString>(1); }
Metadata *getRawFile() const { return getOperand(2); }
Metadata *getRawType() const { return getOperand(3); }
static bool classof(const Metadata *MD) { static bool classof(const Metadata *MD) {
return MD->getMetadataID() == MDLocalVariableKind || return MD->getMetadataID() == MDLocalVariableKind ||
@ -1347,10 +1350,10 @@ class MDGlobalVariable : public MDVariable {
~MDGlobalVariable() {} ~MDGlobalVariable() {}
static MDGlobalVariable * static MDGlobalVariable *
getImpl(LLVMContext &Context, Metadata *Scope, StringRef Name, getImpl(LLVMContext &Context, MDScope *Scope, StringRef Name,
StringRef LinkageName, Metadata *File, unsigned Line, Metadata *Type, StringRef LinkageName, MDFile *File, unsigned Line, Metadata *Type,
bool IsLocalToUnit, bool IsDefinition, Metadata *Variable, bool IsLocalToUnit, bool IsDefinition, ConstantAsMetadata *Variable,
Metadata *StaticDataMemberDeclaration, StorageType Storage, MDDerivedType *StaticDataMemberDeclaration, StorageType Storage,
bool ShouldCreate = true) { bool ShouldCreate = true) {
return getImpl(Context, Scope, getCanonicalMDString(Context, Name), return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
getCanonicalMDString(Context, LinkageName), File, Line, Type, getCanonicalMDString(Context, LinkageName), File, Line, Type,
@ -1373,10 +1376,11 @@ class MDGlobalVariable : public MDVariable {
public: public:
DEFINE_MDNODE_GET(MDGlobalVariable, DEFINE_MDNODE_GET(MDGlobalVariable,
(Metadata * Scope, StringRef Name, StringRef LinkageName, (MDScope * Scope, StringRef Name, StringRef LinkageName,
Metadata *File, unsigned Line, Metadata *Type, MDFile *File, unsigned Line, Metadata *Type,
bool IsLocalToUnit, bool IsDefinition, Metadata *Variable, bool IsLocalToUnit, bool IsDefinition,
Metadata *StaticDataMemberDeclaration), ConstantAsMetadata *Variable,
MDDerivedType *StaticDataMemberDeclaration),
(Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
IsDefinition, Variable, StaticDataMemberDeclaration)) IsDefinition, Variable, StaticDataMemberDeclaration))
DEFINE_MDNODE_GET(MDGlobalVariable, DEFINE_MDNODE_GET(MDGlobalVariable,
@ -1393,10 +1397,16 @@ public:
bool isDefinition() const { return IsDefinition; } bool isDefinition() const { return IsDefinition; }
StringRef getDisplayName() const { return getStringOperand(4); } StringRef getDisplayName() const { return getStringOperand(4); }
StringRef getLinkageName() const { return getStringOperand(5); } StringRef getLinkageName() const { return getStringOperand(5); }
Metadata *getVariable() const { return getOperand(6); } ConstantAsMetadata *getVariable() const {
Metadata *getStaticDataMemberDeclaration() const { return getOperand(7); } return cast_or_null<ConstantAsMetadata>(getRawVariable());
}
MDDerivedType *getStaticDataMemberDeclaration() const {
return cast_or_null<MDDerivedType>(getRawStaticDataMemberDeclaration());
}
MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); } MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
Metadata *getRawVariable() const { return getOperand(6); }
Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(7); }
static bool classof(const Metadata *MD) { static bool classof(const Metadata *MD) {
return MD->getMetadataID() == MDGlobalVariableKind; return MD->getMetadataID() == MDGlobalVariableKind;
@ -1423,10 +1433,10 @@ class MDLocalVariable : public MDVariable {
~MDLocalVariable() {} ~MDLocalVariable() {}
static MDLocalVariable *getImpl(LLVMContext &Context, unsigned Tag, static MDLocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
Metadata *Scope, StringRef Name, MDScope *Scope, StringRef Name, MDFile *File,
Metadata *File, unsigned Line, Metadata *Type, unsigned Line, Metadata *Type, unsigned Arg,
unsigned Arg, unsigned Flags, unsigned Flags, MDLocation *InlinedAt,
Metadata *InlinedAt, StorageType Storage, StorageType Storage,
bool ShouldCreate = true) { bool ShouldCreate = true) {
return getImpl(Context, Tag, Scope, getCanonicalMDString(Context, Name), return getImpl(Context, Tag, Scope, getCanonicalMDString(Context, Name),
File, Line, Type, Arg, Flags, InlinedAt, Storage, File, Line, Type, Arg, Flags, InlinedAt, Storage,
@ -1447,10 +1457,9 @@ class MDLocalVariable : public MDVariable {
public: public:
DEFINE_MDNODE_GET(MDLocalVariable, DEFINE_MDNODE_GET(MDLocalVariable,
(unsigned Tag, Metadata *Scope, StringRef Name, (unsigned Tag, MDLocalScope *Scope, StringRef Name,
Metadata *File, unsigned Line, Metadata *Type, MDFile *File, unsigned Line, Metadata *Type, unsigned Arg,
unsigned Arg, unsigned Flags, unsigned Flags, MDLocation *InlinedAt = nullptr),
Metadata *InlinedAt = nullptr),
(Tag, Scope, Name, File, Line, Type, Arg, Flags, InlinedAt)) (Tag, Scope, Name, File, Line, Type, Arg, Flags, InlinedAt))
DEFINE_MDNODE_GET(MDLocalVariable, DEFINE_MDNODE_GET(MDLocalVariable,
(unsigned Tag, Metadata *Scope, MDString *Name, (unsigned Tag, Metadata *Scope, MDString *Name,
@ -1461,9 +1470,20 @@ public:
TempMDLocalVariable clone() const { return cloneImpl(); } TempMDLocalVariable clone() const { return cloneImpl(); }
/// \brief Get the local scope for this variable.
///
/// Variables must be defined in a local scope.
MDLocalScope *getScope() const {
return cast<MDLocalScope>(MDVariable::getScope());
}
unsigned getArg() const { return Arg; } unsigned getArg() const { return Arg; }
unsigned getFlags() const { return Flags; } unsigned getFlags() const { return Flags; }
Metadata *getInlinedAt() const { return getOperand(4); } MDLocation *getInlinedAt() const {
return cast_or_null<MDLocation>(getRawInlinedAt());
}
Metadata *getRawInlinedAt() const { return getOperand(4); }
/// \brief Get an inlined version of this variable. /// \brief Get an inlined version of this variable.
/// ///

View File

@ -3035,8 +3035,8 @@ struct MDField : public MDFieldImpl<Metadata *> {
struct MDConstant : public MDFieldImpl<ConstantAsMetadata *> { struct MDConstant : public MDFieldImpl<ConstantAsMetadata *> {
MDConstant() : ImplTy(nullptr) {} MDConstant() : ImplTy(nullptr) {}
}; };
struct MDStringField : public MDFieldImpl<std::string> { struct MDStringField : public MDFieldImpl<MDString *> {
MDStringField() : ImplTy(std::string()) {} MDStringField() : ImplTy(nullptr) {}
}; };
struct MDFieldList : public MDFieldImpl<SmallVector<Metadata *, 4>> { struct MDFieldList : public MDFieldImpl<SmallVector<Metadata *, 4>> {
MDFieldList() : ImplTy(SmallVector<Metadata *, 4>()) {} MDFieldList() : ImplTy(SmallVector<Metadata *, 4>()) {}
@ -3250,7 +3250,7 @@ bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDStringField &Result) {
if (ParseStringConstant(S)) if (ParseStringConstant(S))
return true; return true;
Result.assign(std::move(S)); Result.assign(S.empty() ? nullptr : MDString::get(Context, S));
return false; return false;
} }
@ -3675,7 +3675,7 @@ bool LLParser::ParseMDGlobalVariable(MDNode *&Result, bool IsDistinct) {
bool LLParser::ParseMDLocalVariable(MDNode *&Result, bool IsDistinct) { bool LLParser::ParseMDLocalVariable(MDNode *&Result, bool IsDistinct) {
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
REQUIRED(tag, DwarfTagField, ); \ REQUIRED(tag, DwarfTagField, ); \
OPTIONAL(scope, MDField, ); \ REQUIRED(scope, MDField, ); \
OPTIONAL(name, MDStringField, ); \ OPTIONAL(name, MDStringField, ); \
OPTIONAL(file, MDField, ); \ OPTIONAL(file, MDField, ); \
OPTIONAL(line, LineField, ); \ OPTIONAL(line, LineField, ); \

View File

@ -1720,14 +1720,14 @@ static void writeMDGlobalVariable(raw_ostream &Out, const MDGlobalVariable *N,
MDFieldPrinter Printer(Out, TypePrinter, Machine, Context); MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Printer.printString("name", N->getName()); Printer.printString("name", N->getName());
Printer.printString("linkageName", N->getLinkageName()); Printer.printString("linkageName", N->getLinkageName());
Printer.printMetadata("scope", N->getScope(), /* ShouldSkipNull */ false); Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
Printer.printMetadata("file", N->getFile()); Printer.printMetadata("file", N->getRawFile());
Printer.printInt("line", N->getLine()); Printer.printInt("line", N->getLine());
Printer.printMetadata("type", N->getType()); Printer.printMetadata("type", N->getRawType());
Printer.printBool("isLocal", N->isLocalToUnit()); Printer.printBool("isLocal", N->isLocalToUnit());
Printer.printBool("isDefinition", N->isDefinition()); Printer.printBool("isDefinition", N->isDefinition());
Printer.printMetadata("variable", N->getVariable()); Printer.printMetadata("variable", N->getRawVariable());
Printer.printMetadata("declaration", N->getStaticDataMemberDeclaration()); Printer.printMetadata("declaration", N->getRawStaticDataMemberDeclaration());
Out << ")"; Out << ")";
} }
@ -1741,12 +1741,12 @@ static void writeMDLocalVariable(raw_ostream &Out, const MDLocalVariable *N,
Printer.printInt("arg", N->getArg(), Printer.printInt("arg", N->getArg(),
/* ShouldSkipZero */ /* ShouldSkipZero */
N->getTag() == dwarf::DW_TAG_auto_variable); N->getTag() == dwarf::DW_TAG_auto_variable);
Printer.printMetadata("scope", N->getScope(), /* ShouldSkipNull */ false); Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
Printer.printMetadata("file", N->getFile()); Printer.printMetadata("file", N->getRawFile());
Printer.printInt("line", N->getLine()); Printer.printInt("line", N->getLine());
Printer.printMetadata("type", N->getType()); Printer.printMetadata("type", N->getRawType());
Printer.printDIFlags("flags", N->getFlags()); Printer.printDIFlags("flags", N->getFlags());
Printer.printMetadata("inlinedAt", N->getInlinedAt()); Printer.printMetadata("inlinedAt", N->getRawInlinedAt());
Out << ")"; Out << ")";
} }

View File

@ -582,9 +582,10 @@ DIGlobalVariable DIBuilder::createGlobalVariable(
MDNode *Decl) { MDNode *Decl) {
checkGlobalVariableScope(Context); checkGlobalVariableScope(Context);
auto *N = MDGlobalVariable::get(VMContext, Context, Name, LinkageName, F, auto *N = MDGlobalVariable::get(
LineNumber, Ty, isLocalToUnit, true, VMContext, cast_or_null<MDScope>(Context.get()), Name, LinkageName, F,
getConstantOrNull(Val), Decl); LineNumber, Ty, isLocalToUnit, true, getConstantOrNull(Val),
cast_or_null<MDDerivedType>(Decl));
AllGVs.push_back(N); AllGVs.push_back(N);
return N; return N;
} }
@ -595,9 +596,10 @@ DIGlobalVariable DIBuilder::createTempGlobalVariableFwdDecl(
MDNode *Decl) { MDNode *Decl) {
checkGlobalVariableScope(Context); checkGlobalVariableScope(Context);
return MDGlobalVariable::getTemporary(VMContext, Context, Name, LinkageName, return MDGlobalVariable::getTemporary(
F, LineNumber, Ty, isLocalToUnit, false, VMContext, cast_or_null<MDScope>(Context.get()), Name, LinkageName,
getConstantOrNull(Val), Decl).release(); F, LineNumber, Ty, isLocalToUnit, false, getConstantOrNull(Val),
cast_or_null<MDDerivedType>(Decl)).release();
} }
DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope, DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
@ -613,9 +615,9 @@ DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
assert((!Context || Context.isScope()) && assert((!Context || Context.isScope()) &&
"createLocalVariable should be called with a valid Context"); "createLocalVariable should be called with a valid Context");
auto *Node = auto *Node = MDLocalVariable::get(VMContext, Tag,
MDLocalVariable::get(VMContext, Tag, getNonCompileUnitScope(Scope), Name, cast_or_null<MDLocalScope>(Context.get()),
File, LineNo, Ty, ArgNo, Flags); Name, File, LineNo, Ty, ArgNo, Flags);
if (AlwaysPreserve) { if (AlwaysPreserve) {
// The optimizer may remove local variable. If there is an interest // The optimizer may remove local variable. If there is an interest
// to preserve variable info in such situation then stash it in a // to preserve variable info in such situation then stash it in a

View File

@ -699,21 +699,22 @@ template <> struct MDNodeKeyImpl<MDGlobalVariable> {
IsDefinition(IsDefinition), Variable(Variable), IsDefinition(IsDefinition), Variable(Variable),
StaticDataMemberDeclaration(StaticDataMemberDeclaration) {} StaticDataMemberDeclaration(StaticDataMemberDeclaration) {}
MDNodeKeyImpl(const MDGlobalVariable *N) MDNodeKeyImpl(const MDGlobalVariable *N)
: Scope(N->getScope()), Name(N->getName()), : Scope(N->getRawScope()), Name(N->getName()),
LinkageName(N->getLinkageName()), File(N->getFile()), LinkageName(N->getLinkageName()), File(N->getRawFile()),
Line(N->getLine()), Type(N->getType()), Line(N->getLine()), Type(N->getRawType()),
IsLocalToUnit(N->isLocalToUnit()), IsDefinition(N->isDefinition()), IsLocalToUnit(N->isLocalToUnit()), IsDefinition(N->isDefinition()),
Variable(N->getVariable()), Variable(N->getRawVariable()),
StaticDataMemberDeclaration(N->getStaticDataMemberDeclaration()) {} StaticDataMemberDeclaration(N->getRawStaticDataMemberDeclaration()) {}
bool isKeyOf(const MDGlobalVariable *RHS) const { bool isKeyOf(const MDGlobalVariable *RHS) const {
return Scope == RHS->getScope() && Name == RHS->getName() && return Scope == RHS->getRawScope() && Name == RHS->getName() &&
LinkageName == RHS->getLinkageName() && File == RHS->getFile() && LinkageName == RHS->getLinkageName() && File == RHS->getRawFile() &&
Line == RHS->getLine() && Type == RHS->getType() && Line == RHS->getLine() && Type == RHS->getRawType() &&
IsLocalToUnit == RHS->isLocalToUnit() && IsLocalToUnit == RHS->isLocalToUnit() &&
IsDefinition == RHS->isDefinition() && IsDefinition == RHS->isDefinition() &&
Variable == RHS->getVariable() && Variable == RHS->getRawVariable() &&
StaticDataMemberDeclaration == RHS->getStaticDataMemberDeclaration(); StaticDataMemberDeclaration ==
RHS->getRawStaticDataMemberDeclaration();
} }
unsigned getHashValue() const { unsigned getHashValue() const {
return hash_combine(Scope, Name, LinkageName, File, Line, Type, return hash_combine(Scope, Name, LinkageName, File, Line, Type,
@ -739,16 +740,17 @@ template <> struct MDNodeKeyImpl<MDLocalVariable> {
: Tag(Tag), Scope(Scope), Name(Name), File(File), Line(Line), Type(Type), : Tag(Tag), Scope(Scope), Name(Name), File(File), Line(Line), Type(Type),
Arg(Arg), Flags(Flags), InlinedAt(InlinedAt) {} Arg(Arg), Flags(Flags), InlinedAt(InlinedAt) {}
MDNodeKeyImpl(const MDLocalVariable *N) MDNodeKeyImpl(const MDLocalVariable *N)
: Tag(N->getTag()), Scope(N->getScope()), Name(N->getName()), : Tag(N->getTag()), Scope(N->getRawScope()), Name(N->getName()),
File(N->getFile()), Line(N->getLine()), Type(N->getType()), File(N->getRawFile()), Line(N->getLine()), Type(N->getRawType()),
Arg(N->getArg()), Flags(N->getFlags()), InlinedAt(N->getInlinedAt()) {} Arg(N->getArg()), Flags(N->getFlags()),
InlinedAt(N->getRawInlinedAt()) {}
bool isKeyOf(const MDLocalVariable *RHS) const { bool isKeyOf(const MDLocalVariable *RHS) const {
return Tag == RHS->getTag() && Scope == RHS->getScope() && return Tag == RHS->getTag() && Scope == RHS->getRawScope() &&
Name == RHS->getName() && File == RHS->getFile() && Name == RHS->getName() && File == RHS->getRawFile() &&
Line == RHS->getLine() && Type == RHS->getType() && Line == RHS->getLine() && Type == RHS->getRawType() &&
Arg == RHS->getArg() && Flags == RHS->getFlags() && Arg == RHS->getArg() && Flags == RHS->getFlags() &&
InlinedAt == RHS->getInlinedAt(); InlinedAt == RHS->getRawInlinedAt();
} }
unsigned getHashValue() const { unsigned getHashValue() const {
return hash_combine(Tag, Scope, Name, File, Line, Type, Arg, Flags, return hash_combine(Tag, Scope, Name, File, Line, Type, Arg, Flags,

View File

@ -298,6 +298,7 @@ private:
#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) void visit##CLASS(const CLASS &N); #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) void visit##CLASS(const CLASS &N);
#include "llvm/IR/Metadata.def" #include "llvm/IR/Metadata.def"
void visitMDVariable(const MDVariable &N);
// InstVisitor overrides... // InstVisitor overrides...
using InstVisitor<Verifier>::visit; using InstVisitor<Verifier>::visit;
@ -658,6 +659,15 @@ void Verifier::visitMetadataAsValue(const MetadataAsValue &MDV, Function *F) {
visitValueAsMetadata(*V, F); visitValueAsMetadata(*V, F);
} }
/// \brief Check if a value can be a reference to a type.
static bool isTypeRef(const Metadata *MD) {
if (!MD)
return true;
if (auto *S = dyn_cast<MDString>(MD))
return !S->getString().empty();
return isa<MDType>(MD);
}
void Verifier::visitMDLocation(const MDLocation &N) { void Verifier::visitMDLocation(const MDLocation &N) {
Assert(N.getRawScope() && isa<MDLocalScope>(N.getRawScope()), Assert(N.getRawScope() && isa<MDLocalScope>(N.getRawScope()),
"location requires a valid scope", &N, N.getRawScope()); "location requires a valid scope", &N, N.getRawScope());
@ -749,14 +759,42 @@ void Verifier::visitMDTemplateValueParameter(
"invalid tag", &N); "invalid tag", &N);
} }
void Verifier::visitMDVariable(const MDVariable &N) {
if (auto *S = N.getRawScope())
Assert(isa<MDScope>(S), "invalid scope", &N, S);
Assert(isTypeRef(N.getRawType()), "invalid type ref", &N, N.getRawType());
if (auto *F = N.getRawFile())
Assert(isa<MDFile>(F), "invalid file", &N, F);
}
void Verifier::visitMDGlobalVariable(const MDGlobalVariable &N) { void Verifier::visitMDGlobalVariable(const MDGlobalVariable &N) {
// Checks common to all variables.
visitMDVariable(N);
Assert(N.getTag() == dwarf::DW_TAG_variable, "invalid tag", &N); Assert(N.getTag() == dwarf::DW_TAG_variable, "invalid tag", &N);
if (auto *V = N.getRawVariable()) {
Assert(isa<ConstantAsMetadata>(V) &&
!isa<Function>(cast<ConstantAsMetadata>(V)->getValue()),
"invalid global varaible ref", &N, V);
}
if (auto *Member = N.getRawStaticDataMemberDeclaration()) {
Assert(isa<MDDerivedType>(Member), "invalid static data member declaration",
&N, Member);
}
} }
void Verifier::visitMDLocalVariable(const MDLocalVariable &N) { void Verifier::visitMDLocalVariable(const MDLocalVariable &N) {
// Checks common to all variables.
visitMDVariable(N);
Assert(N.getTag() == dwarf::DW_TAG_auto_variable || Assert(N.getTag() == dwarf::DW_TAG_auto_variable ||
N.getTag() == dwarf::DW_TAG_arg_variable, N.getTag() == dwarf::DW_TAG_arg_variable,
"invalid tag", &N); "invalid tag", &N);
Assert(N.getRawScope() && isa<MDLocalScope>(N.getRawScope()),
"local variable requires a valid scope", &N, N.getRawScope());
if (auto *IA = N.getRawInlinedAt())
Assert(isa<MDLocation>(IA), "local variable requires a valid scope", &N,
IA);
} }
void Verifier::visitMDExpression(const MDExpression &N) { void Verifier::visitMDExpression(const MDExpression &N) {

View File

@ -1,4 +0,0 @@
; RUN: not llvm-as < %s -disable-output 2>&1 | FileCheck %s
; CHECK: <stdin>:[[@LINE+1]]:29: error: missing required field 'tag'
!0 = !MDLocalVariable(arg: 7)

View File

@ -0,0 +1,4 @@
; RUN: not llvm-as < %s -disable-output 2>&1 | FileCheck %s
; CHECK: <stdin>:[[@LINE+1]]:48: error: missing required field 'scope'
!0 = !MDLocalVariable(tag: DW_TAG_auto_variable)

View File

@ -0,0 +1,4 @@
; RUN: not llvm-as < %s -disable-output 2>&1 | FileCheck %s
; CHECK: <stdin>:[[@LINE+1]]:45: error: missing required field 'tag'
!0 = !MDLocalVariable(scope: !MDSubprogram())

View File

@ -3,20 +3,25 @@
@foo = global i32 0 @foo = global i32 0
; CHECK: !named = !{!0, !1, !2, !3, !4, !5, !6} ; CHECK: !named = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9}
!named = !{!0, !1, !2, !3, !4, !5, !6} !named = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9}
!0 = distinct !{} !0 = !MDFile(filename: "scope.h", directory: "/path/to/dir")
!1 = distinct !{} !1 = distinct !{}
!2 = !MDFile(filename: "path/to/file", directory: "/path/to/dir") !2 = !MDFile(filename: "path/to/file", directory: "/path/to/dir")
!3 = distinct !{} !3 = !MDBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
!4 = distinct !{} !4 = distinct !{}
; CHECK: !5 = !MDGlobalVariable(name: "foo", linkageName: "foo", scope: !0, file: !2, line: 7, type: !3, isLocal: true, isDefinition: false, variable: i32* @foo, declaration: !4) ; CHECK: !5 = !MDGlobalVariable(name: "foo", linkageName: "foo", scope: !0, file: !2, line: 7, type: !3, isLocal: true, isDefinition: false, variable: i32* @foo)
!5 = !MDGlobalVariable(name: "foo", linkageName: "foo", scope: !0, !5 = !MDGlobalVariable(name: "foo", linkageName: "foo", scope: !0,
file: !2, line: 7, type: !3, isLocal: true, file: !2, line: 7, type: !3, isLocal: true,
isDefinition: false, variable: i32* @foo, isDefinition: false, variable: i32* @foo)
declaration: !4)
; CHECK: !6 = !MDGlobalVariable(scope: null, isLocal: false, isDefinition: true) ; CHECK: !6 = !MDGlobalVariable(scope: !0, isLocal: false, isDefinition: true)
!6 = !MDGlobalVariable() !6 = !MDGlobalVariable(scope: !0)
!7 = !MDCompositeType(tag: DW_TAG_structure_type, name: "Class", size: 8, align: 8)
!8 = !MDDerivedType(tag: DW_TAG_member, name: "mem", flags: DIFlagStaticMember, scope: !7, baseType: !3)
; CHECK: !9 = !MDGlobalVariable(name: "mem", scope: !0, isLocal: false, isDefinition: true, declaration: !8)
!9 = !MDGlobalVariable(name: "mem", scope: !0, declaration: !8)

View File

@ -6,11 +6,11 @@
; CHECK: !named = !{!0, !1, !2, !3, !4, !5, !6, !7, !8} ; CHECK: !named = !{!0, !1, !2, !3, !4, !5, !6, !7, !8}
!named = !{!0, !1, !2, !3, !4, !5, !6, !7, !8} !named = !{!0, !1, !2, !3, !4, !5, !6, !7, !8}
!0 = distinct !{} !0 = distinct !MDSubprogram()
!1 = distinct !{} !1 = distinct !{}
!2 = !MDFile(filename: "path/to/file", directory: "/path/to/dir") !2 = !MDFile(filename: "path/to/file", directory: "/path/to/dir")
!3 = distinct !{} !3 = !MDBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
!4 = distinct !{} !4 = !MDLocation(scope: !0)
; CHECK: !5 = !MDLocalVariable(tag: DW_TAG_arg_variable, name: "foo", arg: 3, scope: !0, file: !2, line: 7, type: !3, flags: DIFlagArtificial, inlinedAt: !4) ; CHECK: !5 = !MDLocalVariable(tag: DW_TAG_arg_variable, name: "foo", arg: 3, scope: !0, file: !2, line: 7, type: !3, flags: DIFlagArtificial, inlinedAt: !4)
; CHECK: !6 = !MDLocalVariable(tag: DW_TAG_auto_variable, name: "foo", scope: !0, file: !2, line: 7, type: !3, flags: DIFlagArtificial, inlinedAt: !4) ; CHECK: !6 = !MDLocalVariable(tag: DW_TAG_auto_variable, name: "foo", scope: !0, file: !2, line: 7, type: !3, flags: DIFlagArtificial, inlinedAt: !4)
@ -20,7 +20,7 @@
!6 = !MDLocalVariable(tag: DW_TAG_auto_variable, name: "foo", scope: !0, !6 = !MDLocalVariable(tag: DW_TAG_auto_variable, name: "foo", scope: !0,
file: !2, line: 7, type: !3, flags: DIFlagArtificial, inlinedAt: !4) file: !2, line: 7, type: !3, flags: DIFlagArtificial, inlinedAt: !4)
; CHECK: !7 = !MDLocalVariable(tag: DW_TAG_arg_variable, arg: 0, scope: null) ; CHECK: !7 = !MDLocalVariable(tag: DW_TAG_arg_variable, arg: 0, scope: !0)
; CHECK: !8 = !MDLocalVariable(tag: DW_TAG_auto_variable, scope: null) ; CHECK: !8 = !MDLocalVariable(tag: DW_TAG_auto_variable, scope: !0)
!7 = !MDLocalVariable(tag: DW_TAG_arg_variable) !7 = !MDLocalVariable(tag: DW_TAG_arg_variable, scope: !0)
!8 = !MDLocalVariable(tag: DW_TAG_auto_variable) !8 = !MDLocalVariable(tag: DW_TAG_auto_variable, scope: !0)

View File

@ -6,7 +6,7 @@
define void @foo(i32 %a) { define void @foo(i32 %a) {
entry: entry:
%s = alloca i32 %s = alloca i32
call void @llvm.dbg.declare(metadata !"", metadata !MDLocalVariable(tag: DW_TAG_arg_variable), metadata !MDExpression()) call void @llvm.dbg.declare(metadata !"", metadata !MDLocalVariable(tag: DW_TAG_arg_variable, scope: !1), metadata !MDExpression())
ret void ret void
} }
@ -14,3 +14,4 @@ declare void @llvm.dbg.declare(metadata, metadata, metadata)
!llvm.module.flags = !{!0} !llvm.module.flags = !{!0}
!0 = !{i32 2, !"Debug Info Version", i32 3} !0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = !MDSubprogram()

View File

@ -6,7 +6,7 @@
define void @foo(i32 %a) { define void @foo(i32 %a) {
entry: entry:
%s = alloca i32 %s = alloca i32
call void @llvm.dbg.declare(metadata i32* %s, metadata !MDLocalVariable(tag: DW_TAG_arg_variable), metadata !"") call void @llvm.dbg.declare(metadata i32* %s, metadata !MDLocalVariable(tag: DW_TAG_arg_variable, scope: !1), metadata !"")
ret void ret void
} }
@ -14,3 +14,4 @@ declare void @llvm.dbg.declare(metadata, metadata, metadata)
!llvm.module.flags = !{!0} !llvm.module.flags = !{!0}
!0 = !{i32 2, !"Debug Info Version", i32 3} !0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = !MDSubprogram()

View File

@ -6,7 +6,7 @@
define void @foo(i32 %a) { define void @foo(i32 %a) {
entry: entry:
%s = alloca i32 %s = alloca i32
call void @llvm.dbg.value(metadata i32* %s, i64 0, metadata !MDLocalVariable(tag: DW_TAG_arg_variable), metadata !"") call void @llvm.dbg.value(metadata i32* %s, i64 0, metadata !MDLocalVariable(tag: DW_TAG_arg_variable, scope: !1), metadata !"")
ret void ret void
} }
@ -14,3 +14,4 @@ declare void @llvm.dbg.value(metadata, i64, metadata, metadata)
!llvm.module.flags = !{!0} !llvm.module.flags = !{!0}
!0 = !{i32 2, !"Debug Info Version", i32 3} !0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = !MDSubprogram()

View File

@ -6,7 +6,7 @@
define void @foo(i32 %a) { define void @foo(i32 %a) {
entry: entry:
%s = alloca i32 %s = alloca i32
call void @llvm.dbg.value(metadata !"", i64 0, metadata !MDLocalVariable(tag: DW_TAG_arg_variable), metadata !MDExpression()) call void @llvm.dbg.value(metadata !"", i64 0, metadata !MDLocalVariable(tag: DW_TAG_arg_variable, scope: !1), metadata !MDExpression())
ret void ret void
} }
@ -14,3 +14,4 @@ declare void @llvm.dbg.value(metadata, i64, metadata, metadata)
!llvm.module.flags = !{!0} !llvm.module.flags = !{!0}
!0 = !{i32 2, !"Debug Info Version", i32 3} !0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = !MDSubprogram()

View File

@ -61,8 +61,13 @@ TEST(ContextAndReplaceableUsesTest, takeReplaceableUses) {
} }
class MetadataTest : public testing::Test { class MetadataTest : public testing::Test {
public:
MetadataTest() : Counter(0) {}
protected: protected:
LLVMContext Context; LLVMContext Context;
int Counter;
MDNode *getNode() { return MDNode::get(Context, None); } MDNode *getNode() { return MDNode::get(Context, None); }
MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); } MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
MDNode *getNode(Metadata *MD1, Metadata *MD2) { MDNode *getNode(Metadata *MD1, Metadata *MD2) {
@ -75,6 +80,21 @@ protected:
nullptr, false, false, 0, nullptr, 0, 0, 0, nullptr, false, false, 0, nullptr, 0, 0, 0,
0); 0);
} }
MDFile *getFile() {
return MDFile::getDistinct(Context, "file.c", "/path/to/dir");
}
MDBasicType *getBasicType(StringRef Name) {
return MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type, Name);
}
MDDerivedType *getDerivedType() {
return MDDerivedType::getDistinct(Context, dwarf::DW_TAG_pointer_type, "",
nullptr, 0, nullptr,
getBasicType("basictype"), 1, 2, 0, 0);
}
ConstantAsMetadata *getConstantAsMetadata() {
return ConstantAsMetadata::get(
ConstantInt::get(Type::getInt32Ty(Context), Counter++));
}
}; };
typedef MetadataTest MDStringTest; typedef MetadataTest MDStringTest;
@ -1646,16 +1666,16 @@ TEST_F(MDTemplateValueParameterTest, get) {
typedef MetadataTest MDGlobalVariableTest; typedef MetadataTest MDGlobalVariableTest;
TEST_F(MDGlobalVariableTest, get) { TEST_F(MDGlobalVariableTest, get) {
Metadata *Scope = MDTuple::getDistinct(Context, None); MDScope *Scope = getSubprogram();
StringRef Name = "name"; StringRef Name = "name";
StringRef LinkageName = "linkage"; StringRef LinkageName = "linkage";
Metadata *File = MDTuple::getDistinct(Context, None); MDFile *File = getFile();
unsigned Line = 5; unsigned Line = 5;
Metadata *Type = MDTuple::getDistinct(Context, None); Metadata *Type = MDTuple::getDistinct(Context, None);
bool IsLocalToUnit = false; bool IsLocalToUnit = false;
bool IsDefinition = true; bool IsDefinition = true;
Metadata *Variable = MDTuple::getDistinct(Context, None); ConstantAsMetadata *Variable = getConstantAsMetadata();
Metadata *StaticDataMemberDeclaration = MDTuple::getDistinct(Context, None); MDDerivedType *StaticDataMemberDeclaration = getDerivedType();
auto *N = MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line, auto *N = MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Type, IsLocalToUnit, IsDefinition, Variable, Type, IsLocalToUnit, IsDefinition, Variable,
@ -1675,8 +1695,9 @@ TEST_F(MDGlobalVariableTest, get) {
Line, Type, IsLocalToUnit, IsDefinition, Line, Type, IsLocalToUnit, IsDefinition,
Variable, StaticDataMemberDeclaration)); Variable, StaticDataMemberDeclaration));
EXPECT_NE(N, MDGlobalVariable::get(Context, File, Name, LinkageName, File, EXPECT_NE(N,
Line, Type, IsLocalToUnit, IsDefinition, MDGlobalVariable::get(Context, getSubprogram(), Name, LinkageName,
File, Line, Type, IsLocalToUnit, IsDefinition,
Variable, StaticDataMemberDeclaration)); Variable, StaticDataMemberDeclaration));
EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, "other", LinkageName, File, EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, "other", LinkageName, File,
Line, Type, IsLocalToUnit, IsDefinition, Line, Type, IsLocalToUnit, IsDefinition,
@ -1684,7 +1705,8 @@ TEST_F(MDGlobalVariableTest, get) {
EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, "other", File, Line, EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, "other", File, Line,
Type, IsLocalToUnit, IsDefinition, Type, IsLocalToUnit, IsDefinition,
Variable, StaticDataMemberDeclaration)); Variable, StaticDataMemberDeclaration));
EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, Scope, EXPECT_NE(N,
MDGlobalVariable::get(Context, Scope, Name, LinkageName, getFile(),
Line, Type, IsLocalToUnit, IsDefinition, Line, Type, IsLocalToUnit, IsDefinition,
Variable, StaticDataMemberDeclaration)); Variable, StaticDataMemberDeclaration));
EXPECT_NE(N, EXPECT_NE(N,
@ -1702,10 +1724,11 @@ TEST_F(MDGlobalVariableTest, get) {
Variable, StaticDataMemberDeclaration)); Variable, StaticDataMemberDeclaration));
EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Line, Type, IsLocalToUnit, IsDefinition, Line, Type, IsLocalToUnit, IsDefinition,
Type, StaticDataMemberDeclaration)); getConstantAsMetadata(),
StaticDataMemberDeclaration));
EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Line, Type, IsLocalToUnit, IsDefinition, Line, Type, IsLocalToUnit, IsDefinition,
Variable, Type)); Variable, getDerivedType()));
TempMDGlobalVariable Temp = N->clone(); TempMDGlobalVariable Temp = N->clone();
EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
@ -1715,16 +1738,15 @@ typedef MetadataTest MDLocalVariableTest;
TEST_F(MDLocalVariableTest, get) { TEST_F(MDLocalVariableTest, get) {
unsigned Tag = dwarf::DW_TAG_arg_variable; unsigned Tag = dwarf::DW_TAG_arg_variable;
Metadata *Scope = MDTuple::getDistinct(Context, None); MDLocalScope *Scope = getSubprogram();
StringRef Name = "name"; StringRef Name = "name";
Metadata *File = MDTuple::getDistinct(Context, None); MDFile *File = getFile();
unsigned Line = 5; unsigned Line = 5;
Metadata *Type = MDTuple::getDistinct(Context, None); Metadata *Type = MDTuple::getDistinct(Context, None);
unsigned Arg = 6; unsigned Arg = 6;
unsigned Flags = 7; unsigned Flags = 7;
Metadata *InlinedAtScope = MDTuple::getDistinct(Context, None); MDLocation *InlinedAt =
Metadata *InlinedAt = MDLocation::getDistinct(Context, 10, 20, getSubprogram());
MDLocation::getDistinct(Context, 10, 20, InlinedAtScope);
auto *N = MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type, auto *N = MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
Arg, Flags, InlinedAt); Arg, Flags, InlinedAt);
@ -1743,11 +1765,11 @@ TEST_F(MDLocalVariableTest, get) {
EXPECT_NE(N, MDLocalVariable::get(Context, dwarf::DW_TAG_auto_variable, Scope, EXPECT_NE(N, MDLocalVariable::get(Context, dwarf::DW_TAG_auto_variable, Scope,
Name, File, Line, Type, Arg, Flags, Name, File, Line, Type, Arg, Flags,
InlinedAt)); InlinedAt));
EXPECT_NE(N, MDLocalVariable::get(Context, Tag, File, Name, File, Line, EXPECT_NE(N, MDLocalVariable::get(Context, Tag, getSubprogram(), Name, File,
Type, Arg, Flags, InlinedAt)); Line, Type, Arg, Flags, InlinedAt));
EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, "other", File, Line, EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, "other", File, Line,
Type, Arg, Flags, InlinedAt)); Type, Arg, Flags, InlinedAt));
EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, Scope, Line, EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, getFile(), Line,
Type, Arg, Flags, InlinedAt)); Type, Arg, Flags, InlinedAt));
EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line + 1, EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line + 1,
Type, Arg, Flags, InlinedAt)); Type, Arg, Flags, InlinedAt));
@ -1757,8 +1779,9 @@ TEST_F(MDLocalVariableTest, get) {
Arg + 1, Flags, InlinedAt)); Arg + 1, Flags, InlinedAt));
EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type, EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
Arg, ~Flags, InlinedAt)); Arg, ~Flags, InlinedAt));
EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type, EXPECT_NE(N, MDLocalVariable::get(
Arg, Flags, Scope)); Context, Tag, Scope, Name, File, Line, Type, Arg, Flags,
MDLocation::getDistinct(Context, 10, 20, getSubprogram())));
TempMDLocalVariable Temp = N->clone(); TempMDLocalVariable Temp = N->clone();
EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));