llvm.org GIT mirror llvm / d411842
Revert "[ADT] Make Twine's copy constructor private." This reverts commit 4e4ee1c507e2707bb3c208e1e1b6551c3015cbf5. This is failing due to some code that isn't built on MSVC so I didn't catch. Not immediately obvious how to fix this at first glance, so I'm reverting for now. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@315536 91177308-0d34-0410-b5e6-96231b3b80d8 Zachary Turner 1 year, 11 months ago
17 changed file(s) with 47 addition(s) and 72 deletion(s). Raw diff Collapse all Expand all
7878 /// overloads) to guarantee that particularly important cases (cstring plus
7979 /// StringRef) codegen as desired.
8080 class Twine {
81 friend Twine operator+(const char *LHS, const StringRef &RHS);
82 friend Twine operator+(const StringRef &LHS, const char *RHS);
83 friend Twine operator+(const StringRef &LHS, const StringRef &RHS);
84
8581 /// NodeKind - Represent the type of an argument.
8682 enum NodeKind : unsigned char {
8783 /// An empty string; the result of concatenating anything with it is also
172168 assert(isNullary() && "Invalid kind!");
173169 }
174170
175 // While there are some valid use cases for copying Twines, most of them
176 // are confined to the implementation of Twine itself, and Twine itself is
177 // not intended to be publicly copyable since it can very easily lead to
178 // dangling pointers / references.
179 Twine(const Twine &) = default;
180
181171 /// Construct a binary twine.
182172 explicit Twine(const Twine &LHS, const Twine &RHS)
183173 : LHSKind(TwineKind), RHSKind(TwineKind) {
264254 /*implicit*/ Twine() : LHSKind(EmptyKind), RHSKind(EmptyKind) {
265255 assert(isValid() && "Invalid twine!");
266256 }
257
258 Twine(const Twine &) = default;
267259
268260 /// Construct from a C string.
269261 ///
281273 assert(isValid() && "Invalid twine!");
282274 }
283275
284 Twine(Twine &&Other) = default;
285
286276 /// Construct from an std::string.
287277 /*implicit*/ Twine(const std::string &Str)
288278 : LHSKind(StdStringKind), RHSKind(EmptyKind) {
383373 : LHSKind(StringRefKind), RHSKind(CStringKind) {
384374 this->LHS.stringRef = &LHS;
385375 this->RHS.cString = RHS;
386 assert(isValid() && "Invalid twine!");
387 }
388
389 /// Construct as the concatenation of two StringRefs.
390 /*implicit*/ Twine(const StringRef &LHS, const StringRef &RHS)
391 : LHSKind(StringRefKind), RHSKind(StringRefKind) {
392 this->LHS.stringRef = &LHS;
393 this->RHS.stringRef = &RHS;
394376 assert(isValid() && "Invalid twine!");
395377 }
396378
504486 /// Dump the representation of this twine to stderr.
505487 void dumpRepr() const;
506488
507 friend inline Twine operator+(const Twine &LHS, const Twine &RHS) {
508 return LHS.concat(RHS);
509 }
510
511489 /// @}
512490 };
513491
543521 return Twine(NewLHS, NewLHSKind, NewRHS, NewRHSKind);
544522 }
545523
524 inline Twine operator+(const Twine &LHS, const Twine &RHS) {
525 return LHS.concat(RHS);
526 }
527
546528 /// Additional overload to guarantee simplified codegen; this is equivalent to
547529 /// concat().
548530
550532 return Twine(LHS, RHS);
551533 }
552534
535 /// Additional overload to guarantee simplified codegen; this is equivalent to
536 /// concat().
537
553538 inline Twine operator+(const StringRef &LHS, const char *RHS) {
554 return Twine(LHS, RHS);
555 }
556
557 inline Twine operator+(const StringRef &LHS, const StringRef &RHS) {
558539 return Twine(LHS, RHS);
559540 }
560541
961961 /// Diagnostic information for unsupported feature in backend.
962962 class DiagnosticInfoUnsupported : public DiagnosticInfoWithLocationBase {
963963 private:
964 std::string Msg;
964 Twine Msg;
965965
966966 public:
967967 /// \p Fn is the function where the diagnostic is being emitted. \p Loc is
975975 const DiagnosticLocation &Loc = DiagnosticLocation(),
976976 DiagnosticSeverity Severity = DS_Error)
977977 : DiagnosticInfoWithLocationBase(DK_Unsupported, Severity, Fn, Loc),
978 Msg(Msg.str()) {}
978 Msg(Msg) {}
979979
980980 static bool classof(const DiagnosticInfo *DI) {
981981 return DI->getKind() == DK_Unsupported;
982982 }
983983
984 StringRef getMessage() const { return Msg; }
984 const Twine &getMessage() const { return Msg; }
985985
986986 void print(DiagnosticPrinter &DP) const override;
987987 };
6464 class GenericBinaryError : public ErrorInfo {
6565 public:
6666 static char ID;
67 GenericBinaryError(const Twine &Msg);
68 GenericBinaryError(const Twine &Msg, object_error ECOverride);
67 GenericBinaryError(Twine Msg);
68 GenericBinaryError(Twine Msg, object_error ECOverride);
6969 const std::string &getMessage() const { return Msg; }
7070 void log(raw_ostream &OS) const override;
7171 private:
8686
8787 class EmptyResError : public GenericBinaryError {
8888 public:
89 EmptyResError(const Twine &Msg, object_error ECOverride)
89 EmptyResError(Twine Msg, object_error ECOverride)
9090 : GenericBinaryError(Msg, ECOverride) {}
9191 };
9292
930930 /// This is useful in the base level of your program to allow clean termination
931931 /// (allowing clean deallocation of resources, etc.), while reporting error
932932 /// information to the user.
933 void logAllUnhandledErrors(Error E, raw_ostream &OS, const Twine &ErrorBanner);
933 void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner);
934934
935935 /// Write all error messages (if any) in E to a string. The newline character
936936 /// is used to separate error messages.
2727 };
2828
2929 template class provider_format_adapter : public format_adapter {
30 T &Item;
30 T Item;
3131
3232 public:
3333 explicit provider_format_adapter(T &&Item) : Item(Item) {}
5959 char BinaryError::ID = 0;
6060 char GenericBinaryError::ID = 0;
6161
62 GenericBinaryError::GenericBinaryError(const Twine &Msg) : Msg(Msg.str()) {}
62 GenericBinaryError::GenericBinaryError(Twine Msg) : Msg(Msg.str()) {}
6363
64 GenericBinaryError::GenericBinaryError(const Twine &Msg,
65 object_error ECOverride)
64 GenericBinaryError::GenericBinaryError(Twine Msg, object_error ECOverride)
6665 : Msg(Msg.str()) {
6766 setErrorCode(make_error_code(ECOverride));
6867 }
5353 char ECError::ID = 0;
5454 char StringError::ID = 0;
5555
56 void logAllUnhandledErrors(Error E, raw_ostream &OS, const Twine &ErrorBanner) {
56 void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner) {
5757 if (!E)
5858 return;
5959 OS << ErrorBanner;
119119 << Ptr.cString << "\"";
120120 break;
121121 case Twine::StdStringKind:
122 OS << "std::string:\"" << *Ptr.stdString << "\"";
122 OS << "std::string:\""
123 << Ptr.stdString << "\"";
123124 break;
124125 case Twine::StringRefKind:
125 OS << "stringref:\"" << *Ptr.stringRef << "\"";
126 OS << "stringref:\""
127 << Ptr.stringRef << "\"";
126128 break;
127129 case Twine::SmallStringKind:
128130 OS << "smallstring:\"" << *Ptr.smallString << "\"";
129129 class IRBuilderPrefixedInserter : public IRBuilderDefaultInserter {
130130 std::string Prefix;
131131
132 const Twine getNameWithPrefix(const Twine &Name) const {
133 return Name.isTriviallyEmpty() ? Name : Prefix + Name;
134 }
135
132136 public:
133137 void SetNamePrefix(const Twine &P) { Prefix = P.str(); }
134138
135139 protected:
136140 void InsertHelper(Instruction *I, const Twine &Name, BasicBlock *BB,
137141 BasicBlock::iterator InsertPt) const {
138 const Twine &Prefixed = Prefix + Name;
139 IRBuilderDefaultInserter::InsertHelper(
140 I, Name.isTriviallyEmpty() ? Name : Prefixed, BB, InsertPt);
142 IRBuilderDefaultInserter::InsertHelper(I, getNameWithPrefix(Name), BB,
143 InsertPt);
141144 }
142145 };
143146
13511354 /// This will return the BasePtr if that is valid, or build a new GEP
13521355 /// instruction using the IRBuilder if GEP-ing is needed.
13531356 static Value *buildGEP(IRBuilderTy &IRB, Value *BasePtr,
1354 SmallVectorImpl &Indices,
1355 const Twine &NamePrefix) {
1357 SmallVectorImpl &Indices, Twine NamePrefix) {
13561358 if (Indices.empty())
13571359 return BasePtr;
13581360
13771379 static Value *getNaturalGEPWithType(IRBuilderTy &IRB, const DataLayout &DL,
13781380 Value *BasePtr, Type *Ty, Type *TargetTy,
13791381 SmallVectorImpl &Indices,
1380 const Twine &NamePrefix) {
1382 Twine NamePrefix) {
13811383 if (Ty == TargetTy)
13821384 return buildGEP(IRB, BasePtr, Indices, NamePrefix);
13831385
14221424 Value *Ptr, Type *Ty, APInt &Offset,
14231425 Type *TargetTy,
14241426 SmallVectorImpl &Indices,
1425 const Twine &NamePrefix) {
1427 Twine NamePrefix) {
14261428 if (Offset == 0)
14271429 return getNaturalGEPWithType(IRB, DL, Ptr, Ty, TargetTy, Indices,
14281430 NamePrefix);
14951497 static Value *getNaturalGEPWithOffset(IRBuilderTy &IRB, const DataLayout &DL,
14961498 Value *Ptr, APInt Offset, Type *TargetTy,
14971499 SmallVectorImpl &Indices,
1498 const Twine &NamePrefix) {
1500 Twine NamePrefix) {
14991501 PointerType *Ty = cast(Ptr->getType());
15001502
15011503 // Don't consider any GEPs through an i8* as natural unless the TargetTy is
15331535 /// a single GEP as possible, thus making each GEP more independent of the
15341536 /// surrounding code.
15351537 static Value *getAdjustedPtr(IRBuilderTy &IRB, const DataLayout &DL, Value *Ptr,
1536 APInt Offset, Type *PointerTy,
1537 const Twine &NamePrefix) {
1538 APInt Offset, Type *PointerTy, Twine NamePrefix) {
15381539 // Even though we don't look through PHI nodes, we could be called on an
15391540 // instruction in an unreachable block, which may be on a cycle.
15401541 SmallPtrSet Visited;
194194 std::string ToolName;
195195 } // anonymous namespace
196196
197 static void error(const Twine &Message, const Twine &Path = Twine()) {
197 static void error(Twine Message, Twine Path = Twine()) {
198198 HadError = true;
199199 errs() << ToolName << ": " << Path << ": " << Message << ".\n";
200200 }
201201
202 static bool error(std::error_code EC, const Twine &Path = Twine()) {
202 static bool error(std::error_code EC, Twine Path = Twine()) {
203203 if (EC) {
204204 error(EC.message(), Path);
205205 return true;
427427 }
428428 }
429429
430 SectionBase *SectionTableRef::getSection(uint16_t Index, const Twine &ErrMsg) {
430 SectionBase *SectionTableRef::getSection(uint16_t Index, Twine ErrMsg) {
431431 if (Index == SHN_UNDEF || Index > Sections.size())
432432 error(ErrMsg);
433433 return Sections[Index - 1].get();
434434 }
435435
436436 template
437 T *SectionTableRef::getSectionOfType(uint16_t Index, const Twine &IndexErrMsg,
438 const Twine &TypeErrMsg) {
437 T *SectionTableRef::getSectionOfType(uint16_t Index, Twine IndexErrMsg,
438 Twine TypeErrMsg) {
439439 if (T *Sec = llvm::dyn_cast(getSection(Index, IndexErrMsg)))
440440 return Sec;
441441 error(TypeErrMsg);
2828 : Sections(Secs) {}
2929 SectionTableRef(const SectionTableRef &) = default;
3030
31 SectionBase *getSection(uint16_t Index, const llvm::Twine &ErrMsg);
31 SectionBase *getSection(uint16_t Index, llvm::Twine ErrMsg);
3232
3333 template
34 T *getSectionOfType(uint16_t Index, const llvm::Twine &IndexErrMsg,
35 const llvm::Twine &TypeErrMsg);
34 T *getSectionOfType(uint16_t Index, llvm::Twine IndexErrMsg,
35 llvm::Twine TypeErrMsg);
3636 };
3737
3838 class SectionBase {
2626
2727 namespace llvm {
2828
29 LLVM_ATTRIBUTE_NORETURN void error(const Twine &Message) {
29 LLVM_ATTRIBUTE_NORETURN void error(Twine Message) {
3030 errs() << ToolName << ": " << Message << ".\n";
3131 errs().flush();
3232 exit(1);
1313
1414 namespace llvm {
1515
16 LLVM_ATTRIBUTE_NORETURN extern void error(const Twine &Message);
16 LLVM_ATTRIBUTE_NORETURN extern void error(Twine Message);
1717
1818 // This is taken from llvm-readobj.
1919 // [see here](llvm/tools/llvm-readobj/llvm-readobj.h:38)
8787 repr(Twine("a").concat(Twine(SmallString<3>("b")).concat(Twine("c")))));
8888 }
8989
90 TEST(TwineTest, Operators) {
91 EXPECT_EQ(R"((Twine cstring:"a" stringref:"b"))", repr("a" + StringRef("b")));
92
93 EXPECT_EQ(R"((Twine stringref:"a" cstring:"b"))", repr(StringRef("a") + "b"));
94 EXPECT_EQ(R"((Twine stringref:"a" stringref:"b"))",
95 repr(StringRef("a") + StringRef("b")));
96 }
97
9890 TEST(TwineTest, toNullTerminatedStringRef) {
9991 SmallString<8> storage;
10092 EXPECT_EQ(0, *Twine("hello").toNullTerminatedStringRef(storage).end());
168168 /// to the class.
169169 static void visitRegisterBankClasses(
170170 CodeGenRegBank &RegisterClassHierarchy, const CodeGenRegisterClass *RC,
171 const Twine &Kind,
171 const Twine Kind,
172172 std::function VisitFn,
173173 SmallPtrSetImpl &VisitedRCs) {
174174
182182
183183 for (const auto &PossibleSubclass : RegisterClassHierarchy.getRegClasses()) {
184184 std::string TmpKind =
185 (Kind + " (" + PossibleSubclass.getName() + ")").str();
185 (Twine(Kind) + " (" + PossibleSubclass.getName() + ")").str();
186186
187187 // Visit each subclass of an explicitly named class.
188188 if (RC != &PossibleSubclass && RC->hasSubClass(&PossibleSubclass))