[llvm] [Support] Refactor SourceMgr location formatting to reduce code duplication (NFC) (PR #213798)

via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 3 22:00:57 PDT 2026


https://github.com/zhangweize9-cyber updated https://github.com/llvm/llvm-project/pull/213798

>From 24f0d4485ce7c4ad3caff6496939df814283bdb2 Mon Sep 17 00:00:00 2001
From: zhangweize9-cyber <zhangweize9 at gmail.com>
Date: Mon, 3 Aug 2026 23:05:32 +0800
Subject: [PATCH] [Support] Refactor SourceMgr location formatting to reduce
 code duplication (NFC)

Extract the common line number formatting logic out of the `IncludePath`
if-else branches to avoid redundancy.

Signed-off-by: zhangweize9-cyber <zhangweize9 at gmail.com>
---
 llvm/lib/Support/SourceMgr.cpp | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Support/SourceMgr.cpp b/llvm/lib/Support/SourceMgr.cpp
index 2c40d313bb267..c1594484a531e 100644
--- a/llvm/lib/Support/SourceMgr.cpp
+++ b/llvm/lib/Support/SourceMgr.cpp
@@ -246,14 +246,12 @@ std::string SourceMgr::getFormattedLocationNoOffset(SMLoc Loc,
   assert(BufferID && "Invalid location!");
   auto FileSpec = getBufferInfo(BufferID).Buffer->getBufferIdentifier();
 
-  if (IncludePath) {
+  if (IncludePath)
     return FileSpec.str() + ":" + std::to_string(FindLineNumber(Loc, BufferID));
-  } else {
-    auto I = FileSpec.find_last_of("/\\");
-    I = (I == FileSpec.size()) ? 0 : (I + 1);
-    return FileSpec.substr(I).str() + ":" +
-           std::to_string(FindLineNumber(Loc, BufferID));
-  }
+  auto I = FileSpec.find_last_of("/\\");
+  I = (I == FileSpec.size()) ? 0 : (I + 1);
+  return FileSpec.substr(I).str() + ":" +
+         std::to_string(FindLineNumber(Loc, BufferID));
 }
 
 /// Given a line and column number in a mapped buffer, turn it into an SMLoc.



More information about the llvm-commits mailing list