ELF: Separate error message generation from call of error() or warning().
authorRui Ueyama <ruiu@google.com>
Wed, 16 Dec 2015 22:26:45 +0000 (22:26 +0000)
committerRui Ueyama <ruiu@google.com>
Wed, 16 Dec 2015 22:26:45 +0000 (22:26 +0000)
Previously reportConflict returned only when the third argument is false.
Now it always returns a value.

llvm-svn: 255829

lld/ELF/SymbolTable.cpp
lld/ELF/SymbolTable.h

index 4c66f02cd18420122ce7052844850ed358477bcd..726bc1cdf3272dfa40a2d71fecb2f1bb8f9c67dc 100644 (file)
@@ -121,14 +121,12 @@ void SymbolTable<ELFT>::addELFFile(ELFFileBase<ELFT> *File) {
 }
 
 template <class ELFT>
-void SymbolTable<ELFT>::reportConflict(const Twine &Message,
-                                       const SymbolBody &Old,
-                                       const SymbolBody &New, bool Warning) {
+std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Old, SymbolBody *New) {
   typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
   typedef typename ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
 
-  const Elf_Sym &OldE = cast<ELFSymbolBody<ELFT>>(Old).Sym;
-  const Elf_Sym &NewE = cast<ELFSymbolBody<ELFT>>(New).Sym;
+  const Elf_Sym &OldE = cast<ELFSymbolBody<ELFT>>(*Old).Sym;
+  const Elf_Sym &NewE = cast<ELFSymbolBody<ELFT>>(*New).Sym;
   ELFFileBase<ELFT> *OldFile = nullptr;
   ELFFileBase<ELFT> *NewFile = nullptr;
 
@@ -140,14 +138,10 @@ void SymbolTable<ELFT>::reportConflict(const Twine &Message,
       NewFile = File.get();
   }
 
-  std::string Msg = (Message + ": " + Old.getName() + " in " +
-                     (OldFile ? OldFile->getName() : "(internal)") + " and " +
-                     (NewFile ? NewFile->getName() : "(internal)"))
-                        .str();
-  if (Warning)
-    warning(Msg);
-  else
-    error(Msg);
+  StringRef Sym = Old->getName();
+  StringRef F1 = OldFile ? OldFile->getName() : "(internal)";
+  StringRef F2 = NewFile ? NewFile->getName() : "(internal)";
+  return (Sym + " in " + F1 + " and " + F2).str();
 }
 
 // This function resolves conflicts if there's an existing symbol with
@@ -178,16 +172,20 @@ template <class ELFT> void SymbolTable<ELFT>::resolve(SymbolBody *New) {
   }
 
   if (New->isTLS() != Existing->isTLS())
-    reportConflict("TLS attribute mismatch for symbol", *Existing, *New, false);
+    error("TLS attribute mismatch for symbol: " + conflictMsg(Existing, New));
 
   // compare() returns -1, 0, or 1 if the lhs symbol is less preferable,
   // equivalent (conflicting), or more preferable, respectively.
   int comp = Existing->compare<ELFT>(New);
+  if (comp == 0) {
+    std::string S = "duplicate symbol: " + conflictMsg(Existing, New);
+    if (!Config->AllowMultipleDefinition)
+      error(S);
+    warning(S);
+    return;
+  }
   if (comp < 0)
     Sym->Body = New;
-  else if (comp == 0)
-    reportConflict("duplicate symbol", *Existing, *New,
-                   Config->AllowMultipleDefinition);
 }
 
 template <class ELFT> Symbol *SymbolTable<ELFT>::insert(SymbolBody *New) {
index 44726efdfdfd0997e01668ead973dd36431e1044..e36ed4e8fbbaf4ad340abbea0bdf2da15bd9d730 100644 (file)
@@ -66,8 +66,7 @@ private:
   void addMemberFile(Lazy *Body);
   void checkCompatibility(std::unique_ptr<InputFile> &File);
   void resolve(SymbolBody *Body);
-  void reportConflict(const Twine &Message, const SymbolBody &Old,
-                      const SymbolBody &New, bool Warning);
+  std::string conflictMsg(SymbolBody *Old, SymbolBody *New);
 
   std::vector<std::unique_ptr<InputFile>> ArchiveFiles;