llvm.org GIT mirror llvm / d72479c
Reject uses of unnamed_addr in declarations. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@123358 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 9 years ago
6 changed file(s) with 60 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
693693 unsigned Visibility) {
694694 unsigned AddrSpace;
695695 bool ThreadLocal, IsConstant, UnnamedAddr;
696 LocTy UnnamedAddrLoc;
696697 LocTy TyLoc;
697698
698699 PATypeHolder Ty(Type::getVoidTy(Context));
699700 if (ParseOptionalToken(lltok::kw_thread_local, ThreadLocal) ||
700701 ParseOptionalAddrSpace(AddrSpace) ||
701 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr) ||
702 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
703 &UnnamedAddrLoc) ||
702704 ParseGlobalType(IsConstant) ||
703705 ParseType(Ty, TyLoc))
704706 return true;
712714 if (ParseGlobalValue(Ty, Init))
713715 return true;
714716 }
717
718 if (!Init && UnnamedAddr)
719 return Error(UnnamedAddrLoc, "only definitions can have unnamed_addr");
715720
716721 if (Ty->isFunctionTy() || Ty->isLabelTy())
717722 return Error(TyLoc, "invalid type for global variable");
26682673
26692674 unsigned Visibility, RetAttrs;
26702675 bool UnnamedAddr;
2676 LocTy UnnamedAddrLoc;
26712677 CallingConv::ID CC;
26722678 PATypeHolder RetType(Type::getVoidTy(Context));
26732679 LocTy RetTypeLoc = Lex.getLoc();
26752681 ParseOptionalVisibility(Visibility) ||
26762682 ParseOptionalCallingConv(CC) ||
26772683 ParseOptionalAttrs(RetAttrs, 1) ||
2678 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr) ||
2684 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
2685 &UnnamedAddrLoc) ||
26792686 ParseType(RetType, RetTypeLoc, true /*void allowed*/))
26802687 return true;
2688
2689 if (!isDefine && UnnamedAddr)
2690 return Error(UnnamedAddrLoc, "only definitions can have unnamed_addr");
26812691
26822692 // Verify that the linkage is ok.
26832693 switch ((GlobalValue::LinkageTypes)Linkage) {
161161 Lex.Lex();
162162 return true;
163163 }
164 bool ParseOptionalToken(lltok::Kind T, bool &Present) {
164 bool ParseOptionalToken(lltok::Kind T, bool &Present, LocTy *Loc = 0) {
165165 if (Lex.getKind() != T) {
166166 Present = false;
167167 } else {
168 if (Loc)
169 *Loc = Lex.getLoc();
168170 Lex.Lex();
169171 Present = true;
170172 }
468468 Assert1(GV.hasExternalLinkage() || GV.hasDLLImportLinkage() ||
469469 GV.hasExternalWeakLinkage(),
470470 "invalid linkage type for global declaration", &GV);
471 Assert1(!GV.hasUnnamedAddr(), "only definitions can have unnamed_addr",
472 &GV);
471473 }
472474
473475 visitGlobalValue(GV);
724726 Assert1(F.hasExternalLinkage() || F.hasDLLImportLinkage() ||
725727 F.hasExternalWeakLinkage(),
726728 "invalid linkage type for function declaration", &F);
729 Assert1(!F.hasUnnamedAddr(), "only definitions can have unnamed_addr", &F);
727730 } else {
728731 // Verify that this function (which has a body) is not named "llvm.*". It
729732 // is not legal to define intrinsics.
0 ; RUN: not llvm-as %s -o /dev/null 2>%t
1 ; RUN: FileCheck -input-file=%t %s
2
3 declare unnamed_addr i32 @zed()
4
5 // CHECK: error: only definitions can have unnamed_addr
6 // CHECK: declare unnamed_addr i32 @zed()
7 // CHECK: ^
0 ; RUN: not llvm-as %s -o /dev/null 2>%t
1 ; RUN: FileCheck -input-file=%t %s
2
3 @foo = external unnamed_addr global i8*
4
5 // CHECK: error: only definitions can have unnamed_addr
6 // CHECK: @foo = external unnamed_addr global i8*
7 // CHECK: ^
6060 EXPECT_TRUE(StringRef(Error).startswith("Alias cannot have unnamed_addr"));
6161 }
6262
63 TEST(VerifierTest, ExternalUnnamedAddr) {
64 LLVMContext &C = getGlobalContext();
65 Module M("M", C);
66 const Type *Ty = Type::getInt8Ty(C);
67 GlobalVariable *GV = new GlobalVariable(M, Ty, true,
68 GlobalValue::ExternalLinkage,
69 NULL, "foo");
70 GV->setUnnamedAddr(true);
71 std::string Error;
72 EXPECT_TRUE(verifyModule(M, ReturnStatusAction, &Error));
73 EXPECT_TRUE(StringRef(Error)
74 .startswith("only definitions can have unnamed_addr"));
75 }
76
77 TEST(VerifierTest, DeclarationUnnamedAddr) {
78 LLVMContext &C = getGlobalContext();
79 Module M("M", C);
80 FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg=*/false);
81 Function *F = Function::Create(FTy, GlobalValue::ExternalLinkage,
82 "foo", &M);
83 F->setUnnamedAddr(true);
84 std::string Error;
85 EXPECT_TRUE(verifyModule(M, ReturnStatusAction, &Error));
86 EXPECT_TRUE(StringRef(Error)
87 .startswith("only definitions can have unnamed_addr"));
6388 }
6489 }
90 }