Skip to content

Conversation

@MatzeB
Copy link
Contributor

@MatzeB MatzeB commented Jul 9, 2025

Avoid (global) using namespace directives in headers as they are bad style.

@MatzeB MatzeB marked this pull request as ready for review July 9, 2025 01:05
@llvmbot llvmbot added the PGO Profile Guided Optimizations label Jul 9, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 9, 2025

@llvm/pr-subscribers-pgo

Author: Matthias Braun (MatzeB)

Changes

Avoid (global) using namespace directives in headers as they are bad style.


Full diff: https://github.com/llvm/llvm-project/pull/147631.diff

9 Files Affected:

  • (modified) llvm/tools/llvm-profgen/CSPreInliner.h (-3)
  • (modified) llvm/tools/llvm-profgen/ErrorHandling.h (+3-1)
  • (modified) llvm/tools/llvm-profgen/PerfReader.cpp (+2-3)
  • (modified) llvm/tools/llvm-profgen/PerfReader.h (-3)
  • (modified) llvm/tools/llvm-profgen/ProfileGenerator.cpp (+3-3)
  • (modified) llvm/tools/llvm-profgen/ProfileGenerator.h (-3)
  • (modified) llvm/tools/llvm-profgen/ProfiledBinary.cpp (+1)
  • (modified) llvm/tools/llvm-profgen/ProfiledBinary.h (+13-18)
  • (modified) llvm/tools/llvm-profgen/llvm-profgen.cpp (+3-3)
diff --git a/llvm/tools/llvm-profgen/CSPreInliner.h b/llvm/tools/llvm-profgen/CSPreInliner.h
index 8a3f16a4f13cb..022c3f8d0daed 100644
--- a/llvm/tools/llvm-profgen/CSPreInliner.h
+++ b/llvm/tools/llvm-profgen/CSPreInliner.h
@@ -16,9 +16,6 @@
 #include "llvm/Transforms/IPO/ProfiledCallGraph.h"
 #include "llvm/Transforms/IPO/SampleContextTracker.h"
 
-using namespace llvm;
-using namespace sampleprof;
-
 namespace llvm {
 namespace sampleprof {
 
diff --git a/llvm/tools/llvm-profgen/ErrorHandling.h b/llvm/tools/llvm-profgen/ErrorHandling.h
index b797add8a892f..17084bd785e64 100644
--- a/llvm/tools/llvm-profgen/ErrorHandling.h
+++ b/llvm/tools/llvm-profgen/ErrorHandling.h
@@ -16,7 +16,7 @@
 #include "llvm/Support/WithColor.h"
 #include <system_error>
 
-using namespace llvm;
+namespace llvm {
 
 [[noreturn]] inline void exitWithError(const Twine &Message,
                                        StringRef Whence = StringRef(),
@@ -53,4 +53,6 @@ inline void emitWarningSummary(uint64_t Num, uint64_t Total, StringRef Msg) {
                        << "%(" << Num << "/" << Total << ") " << Msg << "\n";
 }
 
+} // end namespace llvm
+
 #endif
diff --git a/llvm/tools/llvm-profgen/PerfReader.cpp b/llvm/tools/llvm-profgen/PerfReader.cpp
index ad113eda27914..4ab5f2e63fd12 100644
--- a/llvm/tools/llvm-profgen/PerfReader.cpp
+++ b/llvm/tools/llvm-profgen/PerfReader.cpp
@@ -15,6 +15,8 @@
 
 #define DEBUG_TYPE "perf-reader"
 
+using namespace llvm;
+
 cl::opt<bool> SkipSymbolization("skip-symbolization",
                                 cl::desc("Dump the unsymbolized profile to the "
                                          "output file. It will show unwinder "
@@ -47,9 +49,6 @@ static cl::opt<int> CSProfMaxUnsymbolizedCtxDepth(
     cl::desc("Keep the last K contexts while merging unsymbolized profile. -1 "
              "means no depth limit."));
 
-extern cl::opt<std::string> PerfTraceFilename;
-extern cl::opt<bool> ShowDisassemblyOnly;
-extern cl::opt<bool> ShowSourceLocations;
 extern cl::opt<std::string> OutputFilename;
 
 namespace llvm {
diff --git a/llvm/tools/llvm-profgen/PerfReader.h b/llvm/tools/llvm-profgen/PerfReader.h
index 4b3ac8f569755..19451915812e1 100644
--- a/llvm/tools/llvm-profgen/PerfReader.h
+++ b/llvm/tools/llvm-profgen/PerfReader.h
@@ -17,9 +17,6 @@
 #include <fstream>
 #include <map>
 
-using namespace llvm;
-using namespace sampleprof;
-
 namespace llvm {
 
 class CleanupInstaller;
diff --git a/llvm/tools/llvm-profgen/ProfileGenerator.cpp b/llvm/tools/llvm-profgen/ProfileGenerator.cpp
index db686c3b597eb..9468228acc427 100644
--- a/llvm/tools/llvm-profgen/ProfileGenerator.cpp
+++ b/llvm/tools/llvm-profgen/ProfileGenerator.cpp
@@ -17,6 +17,9 @@
 #include <unordered_set>
 #include <utility>
 
+using namespace llvm;
+using namespace sampleprof;
+
 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
                                     cl::Required,
                                     cl::desc("Output profile file"));
@@ -104,9 +107,6 @@ cl::opt<bool> InferMissingFrames(
         "Infer missing call frames due to compiler tail call elimination."),
     llvm::cl::Optional);
 
-using namespace llvm;
-using namespace sampleprof;
-
 namespace llvm {
 
 namespace sampleprof {
diff --git a/llvm/tools/llvm-profgen/ProfileGenerator.h b/llvm/tools/llvm-profgen/ProfileGenerator.h
index 5e36128530cd9..d3e04563a81c2 100644
--- a/llvm/tools/llvm-profgen/ProfileGenerator.h
+++ b/llvm/tools/llvm-profgen/ProfileGenerator.h
@@ -17,9 +17,6 @@
 #include <memory>
 #include <unordered_set>
 
-using namespace llvm;
-using namespace sampleprof;
-
 namespace llvm {
 namespace sampleprof {
 
diff --git a/llvm/tools/llvm-profgen/ProfiledBinary.cpp b/llvm/tools/llvm-profgen/ProfiledBinary.cpp
index 6847ba1b21b1f..beef4338d5f89 100644
--- a/llvm/tools/llvm-profgen/ProfiledBinary.cpp
+++ b/llvm/tools/llvm-profgen/ProfiledBinary.cpp
@@ -25,6 +25,7 @@
 #define DEBUG_TYPE "load-binary"
 
 using namespace llvm;
+using namespace llvm::object;
 using namespace sampleprof;
 
 cl::opt<bool> ShowDisassemblyOnly("show-disassembly-only",
diff --git a/llvm/tools/llvm-profgen/ProfiledBinary.h b/llvm/tools/llvm-profgen/ProfiledBinary.h
index 0588cb48b2af6..fd2fa5301d31d 100644
--- a/llvm/tools/llvm-profgen/ProfiledBinary.h
+++ b/llvm/tools/llvm-profgen/ProfiledBinary.h
@@ -42,15 +42,10 @@
 #include <vector>
 
 namespace llvm {
+
 extern cl::opt<bool> EnableCSPreInliner;
 extern cl::opt<bool> UseContextCostForPreInliner;
-} // namespace llvm
-
-using namespace llvm;
-using namespace sampleprof;
-using namespace llvm::object;
 
-namespace llvm {
 namespace sampleprof {
 
 class ProfiledBinary;
@@ -303,34 +298,34 @@ class ProfiledBinary {
 
   bool IsCOFF = false;
 
-  void setPreferredTextSegmentAddresses(const ObjectFile *O);
+  void setPreferredTextSegmentAddresses(const object::ObjectFile *O);
 
   template <class ELFT>
-  void setPreferredTextSegmentAddresses(const ELFFile<ELFT> &Obj,
+  void setPreferredTextSegmentAddresses(const object::ELFFile<ELFT> &Obj,
                                         StringRef FileName);
-  void setPreferredTextSegmentAddresses(const COFFObjectFile *Obj,
+  void setPreferredTextSegmentAddresses(const object::COFFObjectFile *Obj,
                                         StringRef FileName);
 
-  void checkPseudoProbe(const ELFObjectFileBase *Obj);
+  void checkPseudoProbe(const object::ELFObjectFileBase *Obj);
 
-  void decodePseudoProbe(const ELFObjectFileBase *Obj);
+  void decodePseudoProbe(const object::ELFObjectFileBase *Obj);
 
   void
-  checkUseFSDiscriminator(const ObjectFile *Obj,
-                          std::map<SectionRef, SectionSymbolsTy> &AllSymbols);
+  checkUseFSDiscriminator(const object::ObjectFile *Obj,
+                          std::map<object::SectionRef, SectionSymbolsTy> &AllSymbols);
 
   // Set up disassembler and related components.
-  void setUpDisassembler(const ObjectFile *Obj);
+  void setUpDisassembler(const object::ObjectFile *Obj);
   symbolize::LLVMSymbolizer::Options getSymbolizerOpts() const;
 
   // Load debug info of subprograms from DWARF section.
-  void loadSymbolsFromDWARF(ObjectFile &Obj);
+  void loadSymbolsFromDWARF(object::ObjectFile &Obj);
 
   // Load debug info from DWARF unit.
   void loadSymbolsFromDWARFUnit(DWARFUnit &CompilationUnit);
 
   // Create elf symbol to its start address mapping.
-  void populateElfSymbolAddressList(const ELFObjectFileBase *O);
+  void populateElfSymbolAddressList(const object::ELFObjectFileBase *O);
 
   // A function may be spilt into multiple non-continuous address ranges. We use
   // this to set whether start a function range is the real entry of the
@@ -341,11 +336,11 @@ class ProfiledBinary {
   void warnNoFuncEntry();
 
   /// Dissassemble the text section and build various address maps.
-  void disassemble(const ObjectFile *O);
+  void disassemble(const object::ObjectFile *O);
 
   /// Helper function to dissassemble the symbol and extract info for unwinding
   bool dissassembleSymbol(std::size_t SI, ArrayRef<uint8_t> Bytes,
-                          SectionSymbolsTy &Symbols, const SectionRef &Section);
+                          SectionSymbolsTy &Symbols, const object::SectionRef &Section);
   /// Symbolize a given instruction pointer and return a full call context.
   SampleContextFrameVector symbolize(const InstructionPointer &IP,
                                      bool UseCanonicalFnName = false,
diff --git a/llvm/tools/llvm-profgen/llvm-profgen.cpp b/llvm/tools/llvm-profgen/llvm-profgen.cpp
index 3b974e25103ad..5464888e77ad5 100644
--- a/llvm/tools/llvm-profgen/llvm-profgen.cpp
+++ b/llvm/tools/llvm-profgen/llvm-profgen.cpp
@@ -21,6 +21,9 @@
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/Support/VirtualFileSystem.h"
 
+using namespace llvm;
+using namespace sampleprof;
+
 static cl::OptionCategory ProfGenCategory("ProfGen Options");
 
 static cl::opt<std::string> PerfScriptFilename(
@@ -71,9 +74,6 @@ extern cl::opt<bool> ShowDisassemblyOnly;
 extern cl::opt<bool> ShowSourceLocations;
 extern cl::opt<bool> SkipSymbolization;
 
-using namespace llvm;
-using namespace sampleprof;
-
 // Validate the command line input.
 static void validateCommandLine() {
   // Allow the missing perfscript if we only use to show binary disassembly.

@github-actions
Copy link

github-actions bot commented Jul 9, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

(global) `using namespace` directives in headers are bad style.
@MatzeB MatzeB force-pushed the profgen_namespaces branch from 7ca6921 to efcedbf Compare July 9, 2025 01:10
Copy link
Member

@WenleiHe WenleiHe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, thanks for the cleanup

@MatzeB MatzeB enabled auto-merge (squash) August 18, 2025 18:28
@MatzeB MatzeB merged commit 43df97a into llvm:main Aug 18, 2025
9 checks passed
@MatzeB MatzeB deleted the profgen_namespaces branch August 18, 2025 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PGO Profile Guided Optimizations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants