[lld] 1865f0e - [lld-link] Replace warn(...) with Warn(ctx)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 12 22:27:50 PST 2024


Author: Fangrui Song
Date: 2024-12-12T22:27:45-08:00
New Revision: 1865f0e203d4b23e676fb6ce72cf8797d0f0b80a

URL: https://github.com/llvm/llvm-project/commit/1865f0e203d4b23e676fb6ce72cf8797d0f0b80a
DIFF: https://github.com/llvm/llvm-project/commit/1865f0e203d4b23e676fb6ce72cf8797d0f0b80a.diff

LOG: [lld-link] Replace warn(...) with Warn(ctx)

Added: 
    

Modified: 
    lld/COFF/PDB.cpp
    lld/COFF/Writer.cpp

Removed: 
    


################################################################################
diff  --git a/lld/COFF/PDB.cpp b/lld/COFF/PDB.cpp
index 4382dd677ff59a..6dcab906b35bf7 100644
--- a/lld/COFF/PDB.cpp
+++ b/lld/COFF/PDB.cpp
@@ -183,6 +183,7 @@ struct UnrelocatedFpoData {
 enum : uint32_t { kSymbolStreamMagicSize = 4 };
 
 class DebugSHandler {
+  COFFLinkerContext &ctx;
   PDBLinker &linker;
 
   /// The object file whose .debug$S sections we're processing.
@@ -229,8 +230,8 @@ class DebugSHandler {
                               const DebugSubsectionRecord &ss);
 
 public:
-  DebugSHandler(PDBLinker &linker, ObjFile &file)
-      : linker(linker), file(file) {}
+  DebugSHandler(COFFLinkerContext &ctx, PDBLinker &linker, ObjFile &file)
+      : ctx(ctx), linker(linker), file(file) {}
 
   void handleDebugS(SectionChunk *debugChunk);
 
@@ -384,10 +385,11 @@ void PDBLinker::translateIdSymbols(MutableArrayRef<uint8_t> &recordData,
         }
       }
       if (newType == TypeIndex(SimpleTypeKind::NotTranslated)) {
-        warn(formatv("procedure symbol record for `{0}` in {1} refers to PDB "
-                     "item index {2:X} which is not a valid function ID record",
-                     getSymbolName(CVSymbol(recordData)),
-                     source->file->getName(), ti->getIndex()));
+        Warn(ctx) << formatv(
+            "procedure symbol record for `{0}` in {1} refers to PDB "
+            "item index {2:X} which is not a valid function ID record",
+            getSymbolName(CVSymbol(recordData)), source->file->getName(),
+            ti->getIndex());
       }
       *ti = newType;
     }
@@ -420,11 +422,12 @@ static void scopeStackOpen(SmallVectorImpl<uint32_t> &stack,
 }
 
 // To close a scope, update the record that opened the scope.
-static void scopeStackClose(SmallVectorImpl<uint32_t> &stack,
+static void scopeStackClose(COFFLinkerContext &ctx,
+                            SmallVectorImpl<uint32_t> &stack,
                             std::vector<uint8_t> &storage,
                             uint32_t storageBaseOffset, ObjFile *file) {
   if (stack.empty()) {
-    warn("symbol scopes are not balanced in " + file->getName());
+    Warn(ctx) << "symbol scopes are not balanced in " << file->getName();
     return;
   }
 
@@ -594,7 +597,7 @@ void PDBLinker::analyzeSymbolSubsection(
   cantFail(symData.readBytes(0, symData.getLength(), symsBuffer));
 
   if (symsBuffer.empty())
-    warn("empty symbols subsection in " + file->getName());
+    Warn(ctx) << "empty symbols subsection in " << file->getName();
 
   Error ec = forEachCodeViewRecord<CVSymbol>(
       symsBuffer, [&](CVSymbol sym) -> llvm::Error {
@@ -635,7 +638,7 @@ void PDBLinker::analyzeSymbolSubsection(
   // any partial records, undo that. For globals, we just keep what we have and
   // continue.
   if (ec) {
-    warn("corrupt symbol records in " + file->getName());
+    Warn(ctx) << "corrupt symbol records in " << file->getName();
     moduleSymOffset = moduleSymStart;
     consumeError(std::move(ec));
   }
@@ -678,7 +681,7 @@ Error PDBLinker::writeAllModuleSymbolRecords(ObjFile *file,
             if (symbolOpensScope(sym.kind()))
               scopeStackOpen(scopes, storage);
             else if (symbolEndsScope(sym.kind()))
-              scopeStackClose(scopes, storage, moduleSymStart, file);
+              scopeStackClose(ctx, scopes, storage, moduleSymStart, file);
 
             // Copy, relocate, and rewrite each module symbol.
             if (symbolGoesInModuleStream(sym, scopes.size())) {
@@ -740,12 +743,12 @@ static pdb::SectionContrib createSectionContrib(COFFLinkerContext &ctx,
 }
 
 static uint32_t
-translateStringTableIndex(uint32_t objIndex,
+translateStringTableIndex(COFFLinkerContext &ctx, uint32_t objIndex,
                           const DebugStringTableSubsectionRef &objStrTable,
                           DebugStringTableSubsection &pdbStrTable) {
   auto expectedString = objStrTable.getString(objIndex);
   if (!expectedString) {
-    warn("Invalid string table reference");
+    Warn(ctx) << "Invalid string table reference";
     consumeError(expectedString.takeError());
     return 0;
   }
@@ -820,8 +823,9 @@ void DebugSHandler::handleDebugS(SectionChunk *debugChunk) {
       break;
 
     default:
-      warn("ignoring unknown debug$S subsection kind 0x" +
-           utohexstr(uint32_t(ss.kind())) + " in file " + toString(&file));
+      Warn(ctx) << "ignoring unknown debug$S subsection kind 0x"
+                << utohexstr(uint32_t(ss.kind())) << " in file "
+                << toString(&file);
       break;
     }
   }
@@ -934,8 +938,9 @@ void DebugSHandler::finish() {
             "string table subsection");
 
     if (!stringTableFixups.empty())
-      warn("No StringTable subsection was encountered, but there are string "
-           "table references");
+      Warn(ctx)
+          << "No StringTable subsection was encountered, but there are string "
+             "table references";
     return;
   }
 
@@ -967,8 +972,8 @@ void DebugSHandler::finish() {
     exitOnErr(fds.initialize(reader));
     for (codeview::FrameData fd : fds) {
       fd.RvaStart += rvaStart;
-      fd.FrameFunc =
-          translateStringTableIndex(fd.FrameFunc, cvStrTab, linker.pdbStrTab);
+      fd.FrameFunc = translateStringTableIndex(ctx, fd.FrameFunc, cvStrTab,
+                                               linker.pdbStrTab);
       dbiBuilder.addNewFpoData(fd);
     }
   }
@@ -976,8 +981,8 @@ void DebugSHandler::finish() {
   // Translate the fixups and pass them off to the module builder so they will
   // be applied during writing.
   for (StringTableFixup &ref : stringTableFixups) {
-    ref.StrTabOffset =
-        translateStringTableIndex(ref.StrTabOffset, cvStrTab, linker.pdbStrTab);
+    ref.StrTabOffset = translateStringTableIndex(ctx, ref.StrTabOffset,
+                                                 cvStrTab, linker.pdbStrTab);
   }
   file.moduleDBI->setStringTableFixups(std::move(stringTableFixups));
 
@@ -1032,7 +1037,7 @@ void PDBLinker::addDebugSymbols(TpiSource *source) {
   ScopedTimer t(ctx.symbolMergingTimer);
   ExitOnError exitOnErr;
   pdb::DbiStreamBuilder &dbiBuilder = builder.getDbiBuilder();
-  DebugSHandler dsh(*this, *source->file);
+  DebugSHandler dsh(ctx, *this, *source->file);
   // Now do all live .debug$S and .debug$F sections.
   for (SectionChunk *debugChunk : source->file->getDebugChunks()) {
     if (!debugChunk->live || debugChunk->getSize() == 0)
@@ -1326,7 +1331,7 @@ void PDBLinker::addNatvisFiles() {
     ErrorOr<std::unique_ptr<MemoryBuffer>> dataOrErr =
         MemoryBuffer::getFile(file);
     if (!dataOrErr) {
-      warn("Cannot open input file: " + file);
+      Warn(ctx) << "Cannot open input file: " << file;
       continue;
     }
     std::unique_ptr<MemoryBuffer> data = std::move(*dataOrErr);
@@ -1348,7 +1353,7 @@ void PDBLinker::addNamedStreams() {
     ErrorOr<std::unique_ptr<MemoryBuffer>> dataOrErr =
         MemoryBuffer::getFile(file);
     if (!dataOrErr) {
-      warn("Cannot open input file: " + file);
+      Warn(ctx) << "Cannot open input file: " << file;
       continue;
     }
     std::unique_ptr<MemoryBuffer> data = std::move(*dataOrErr);

diff  --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index 3d38c3b6c241cf..3c6112b7fc89ad 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -2686,7 +2686,7 @@ template <typename T> void Writer::prepareLoadConfig(T *loadConfig) {
 
 #define RETURN_IF_NOT_CONTAINS(field)                                          \
   if (loadConfigSize < offsetof(T, field) + sizeof(T::field)) {                \
-    warn("'_load_config_used' structure too small to include " #field);        \
+    Warn(ctx) << "'_load_config_used' structure too small to include " #field; \
     return;                                                                    \
   }
 
@@ -2696,12 +2696,12 @@ template <typename T> void Writer::prepareLoadConfig(T *loadConfig) {
 #define CHECK_VA(field, sym)                                                   \
   if (auto *s = dyn_cast<DefinedSynthetic>(ctx.symtab.findUnderscore(sym)))    \
     if (loadConfig->field != ctx.config.imageBase + s->getRVA())               \
-      warn(#field " not set correctly in '_load_config_used'");
+      Warn(ctx) << #field " not set correctly in '_load_config_used'";
 
 #define CHECK_ABSOLUTE(field, sym)                                             \
   if (auto *s = dyn_cast<DefinedAbsolute>(ctx.symtab.findUnderscore(sym)))     \
     if (loadConfig->field != s->getVA())                                       \
-      warn(#field " not set correctly in '_load_config_used'");
+      Warn(ctx) << #field " not set correctly in '_load_config_used'";
 
   if (ctx.config.dependentLoadFlags) {
     RETURN_IF_NOT_CONTAINS(DependentLoadFlags)
@@ -2715,8 +2715,8 @@ template <typename T> void Writer::prepareLoadConfig(T *loadConfig) {
           ctx.dynamicRelocs->getRVA() - relocSec->getRVA();
     }
     else {
-      warn("'_load_config_used' structure too small to include dynamic "
-           "relocations");
+      Warn(ctx) << "'_load_config_used' structure too small to include dynamic "
+                   "relocations";
     }
   }
 


        


More information about the llvm-commits mailing list