[clang] [llvm] add comment (PR #103046)

via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 13 05:08:15 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: cratelschen (cratelschen)

<details>
<summary>Changes</summary>



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


3 Files Affected:

- (modified) clang/tools/driver/driver.cpp (+13-9) 
- (modified) llvm/include/llvm/Support/TargetSelect.h (+102-96) 
- (added) test.cpp (+1) 


``````````diff
diff --git a/clang/tools/driver/driver.cpp b/clang/tools/driver/driver.cpp
index 83b5bbb71f5212..62e64b2b282f39 100644
--- a/clang/tools/driver/driver.cpp
+++ b/clang/tools/driver/driver.cpp
@@ -69,7 +69,7 @@ std::string GetExecutablePath(const char *Argv0, bool CanonicalPrefixes) {
 
   // This just needs to be some symbol in the binary; C++ doesn't
   // allow taking the address of ::main however.
-  void *P = (void*) (intptr_t) GetExecutablePath;
+  void *P = (void *)(intptr_t)GetExecutablePath;
   return llvm::sys::fs::getMainExecutable(Argv0, P);
 }
 
@@ -102,10 +102,10 @@ static void insertTargetAndModeArgs(const ParsedClangName &NameParts,
   }
 
   if (NameParts.TargetIsValid) {
-    const char *arr[] = {"-target", GetStableCStr(SavedStrings,
-                                                  NameParts.TargetPrefix)};
-    ArgVector.insert(ArgVector.begin() + InsertionPoint,
-                     std::begin(arr), std::end(arr));
+    const char *arr[] = {"-target",
+                         GetStableCStr(SavedStrings, NameParts.TargetPrefix)};
+    ArgVector.insert(ArgVector.begin() + InsertionPoint, std::begin(arr),
+                     std::end(arr));
   }
 }
 
@@ -225,6 +225,7 @@ static int ExecuteCC1Tool(SmallVectorImpl<const char *> &ArgV,
   return 1;
 }
 
+// Cratels:clang driver的真正入口.main方法在build目录,是有cmake自动生成的.
 int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContext) {
   noteBottomOfStack();
   llvm::setBugReportMsg("PLEASE submit a bug report to " BUG_REPORT_URL
@@ -235,6 +236,9 @@ int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContext) {
   if (llvm::sys::Process::FixupStandardFileDescriptors())
     return 1;
 
+  // clang-format off
+  // Cratels:初始化所有的target.如果在cmake configure的时候指定了target,这只会初始化对应的target,否则就初始化默认的targets.
+  // clang-format on
   llvm::InitializeAllTargets();
 
   llvm::BumpPtrAllocator A;
@@ -316,8 +320,8 @@ int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContext) {
   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts =
       CreateAndPopulateDiagOpts(Args);
 
-  TextDiagnosticPrinter *DiagClient
-    = new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
+  TextDiagnosticPrinter *DiagClient =
+      new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
   FixupDiagPrefixExeName(DiagClient, ProgName);
 
   IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
@@ -423,8 +427,8 @@ int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContext) {
   if (::getenv("FORCE_CLANG_DIAGNOSTICS_CRASH"))
     llvm::dbgs() << llvm::getBugReportMsg();
   if (FailingCommand != nullptr &&
-    TheDriver.maybeGenerateCompilationDiagnostics(CommandStatus, ReproLevel,
-                                                  *C, *FailingCommand))
+      TheDriver.maybeGenerateCompilationDiagnostics(CommandStatus, ReproLevel,
+                                                    *C, *FailingCommand))
     Res = 1;
 
   Diags.getClient()->finish();
diff --git a/llvm/include/llvm/Support/TargetSelect.h b/llvm/include/llvm/Support/TargetSelect.h
index e57614cea758bb..0da7fbd180fb80 100644
--- a/llvm/include/llvm/Support/TargetSelect.h
+++ b/llvm/include/llvm/Support/TargetSelect.h
@@ -18,27 +18,29 @@
 #include "llvm/Config/llvm-config.h"
 
 extern "C" {
-  // Declare all of the target-initialization functions that are available.
+// Declare all of the target-initialization functions that are available.
 #define LLVM_TARGET(TargetName) void LLVMInitialize##TargetName##TargetInfo();
 #include "llvm/Config/Targets.def"
 
 #define LLVM_TARGET(TargetName) void LLVMInitialize##TargetName##Target();
 #include "llvm/Config/Targets.def"
 
-  // Declare all of the target-MC-initialization functions that are available.
+// Declare all of the target-MC-initialization functions that are available.
 #define LLVM_TARGET(TargetName) void LLVMInitialize##TargetName##TargetMC();
 #include "llvm/Config/Targets.def"
 
-  // Declare all of the available assembly printer initialization functions.
-#define LLVM_ASM_PRINTER(TargetName) void LLVMInitialize##TargetName##AsmPrinter();
+// Declare all of the available assembly printer initialization functions.
+#define LLVM_ASM_PRINTER(TargetName)                                           \
+  void LLVMInitialize##TargetName##AsmPrinter();
 #include "llvm/Config/AsmPrinters.def"
 
-  // Declare all of the available assembly parser initialization functions.
-#define LLVM_ASM_PARSER(TargetName) void LLVMInitialize##TargetName##AsmParser();
+// Declare all of the available assembly parser initialization functions.
+#define LLVM_ASM_PARSER(TargetName)                                            \
+  void LLVMInitialize##TargetName##AsmParser();
 #include "llvm/Config/AsmParsers.def"
 
-  // Declare all of the available disassembler initialization functions.
-#define LLVM_DISASSEMBLER(TargetName) \
+// Declare all of the available disassembler initialization functions.
+#define LLVM_DISASSEMBLER(TargetName)                                          \
   void LLVMInitialize##TargetName##Disassembler();
 #include "llvm/Config/Disassemblers.def"
 
@@ -48,129 +50,133 @@ extern "C" {
 }
 
 namespace llvm {
-  /// InitializeAllTargetInfos - The main program should call this function if
-  /// it wants access to all available targets that LLVM is configured to
-  /// support, to make them available via the TargetRegistry.
-  ///
-  /// It is legal for a client to make multiple calls to this function.
-  inline void InitializeAllTargetInfos() {
+/// InitializeAllTargetInfos - The main program should call this function if
+/// it wants access to all available targets that LLVM is configured to
+/// support, to make them available via the TargetRegistry.
+///
+/// It is legal for a client to make multiple calls to this function.
+inline void InitializeAllTargetInfos() {
 #define LLVM_TARGET(TargetName) LLVMInitialize##TargetName##TargetInfo();
 #include "llvm/Config/Targets.def"
-  }
+}
 
-  /// InitializeAllTargets - The main program should call this function if it
-  /// wants access to all available target machines that LLVM is configured to
-  /// support, to make them available via the TargetRegistry.
-  ///
-  /// It is legal for a client to make multiple calls to this function.
-  inline void InitializeAllTargets() {
-    // FIXME: Remove this, clients should do it.
-    InitializeAllTargetInfos();
+/// InitializeAllTargets - The main program should call this function if it
+/// wants access to all available target machines that LLVM is configured to
+/// support, to make them available via the TargetRegistry.
+///
+/// It is legal for a client to make multiple calls to this function.
+inline void InitializeAllTargets() {
+  // FIXME: Remove this, clients should do it.
+  InitializeAllTargetInfos();
 
 #define LLVM_TARGET(TargetName) LLVMInitialize##TargetName##Target();
+  // clang-format off
+  // Cratels:这个def文件是在cmake configure的时候自动生成的,其内容就是需要生成的targets
+  // clang-format on
 #include "llvm/Config/Targets.def"
-  }
-
-  /// InitializeAllTargetMCs - The main program should call this function if it
-  /// wants access to all available target MC that LLVM is configured to
-  /// support, to make them available via the TargetRegistry.
-  ///
-  /// It is legal for a client to make multiple calls to this function.
-  inline void InitializeAllTargetMCs() {
+}
+
+/// InitializeAllTargetMCs - The main program should call this function if it
+/// wants access to all available target MC that LLVM is configured to
+/// support, to make them available via the TargetRegistry.
+///
+/// It is legal for a client to make multiple calls to this function.
+inline void InitializeAllTargetMCs() {
 #define LLVM_TARGET(TargetName) LLVMInitialize##TargetName##TargetMC();
 #include "llvm/Config/Targets.def"
-  }
-
-  /// InitializeAllAsmPrinters - The main program should call this function if
-  /// it wants all asm printers that LLVM is configured to support, to make them
-  /// available via the TargetRegistry.
-  ///
-  /// It is legal for a client to make multiple calls to this function.
-  inline void InitializeAllAsmPrinters() {
+}
+
+/// InitializeAllAsmPrinters - The main program should call this function if
+/// it wants all asm printers that LLVM is configured to support, to make them
+/// available via the TargetRegistry.
+///
+/// It is legal for a client to make multiple calls to this function.
+inline void InitializeAllAsmPrinters() {
 #define LLVM_ASM_PRINTER(TargetName) LLVMInitialize##TargetName##AsmPrinter();
 #include "llvm/Config/AsmPrinters.def"
-  }
-
-  /// InitializeAllAsmParsers - The main program should call this function if it
-  /// wants all asm parsers that LLVM is configured to support, to make them
-  /// available via the TargetRegistry.
-  ///
-  /// It is legal for a client to make multiple calls to this function.
-  inline void InitializeAllAsmParsers() {
+}
+
+/// InitializeAllAsmParsers - The main program should call this function if it
+/// wants all asm parsers that LLVM is configured to support, to make them
+/// available via the TargetRegistry.
+///
+/// It is legal for a client to make multiple calls to this function.
+inline void InitializeAllAsmParsers() {
 #define LLVM_ASM_PARSER(TargetName) LLVMInitialize##TargetName##AsmParser();
 #include "llvm/Config/AsmParsers.def"
-  }
-
-  /// InitializeAllDisassemblers - The main program should call this function if
-  /// it wants all disassemblers that LLVM is configured to support, to make
-  /// them available via the TargetRegistry.
-  ///
-  /// It is legal for a client to make multiple calls to this function.
-  inline void InitializeAllDisassemblers() {
-#define LLVM_DISASSEMBLER(TargetName) LLVMInitialize##TargetName##Disassembler();
+}
+
+/// InitializeAllDisassemblers - The main program should call this function if
+/// it wants all disassemblers that LLVM is configured to support, to make
+/// them available via the TargetRegistry.
+///
+/// It is legal for a client to make multiple calls to this function.
+inline void InitializeAllDisassemblers() {
+#define LLVM_DISASSEMBLER(TargetName)                                          \
+  LLVMInitialize##TargetName##Disassembler();
 #include "llvm/Config/Disassemblers.def"
-  }
-
-  /// InitializeNativeTarget - The main program should call this function to
-  /// initialize the native target corresponding to the host.  This is useful
-  /// for JIT applications to ensure that the target gets linked in correctly.
-  ///
-  /// It is legal for a client to make multiple calls to this function.
-  inline bool InitializeNativeTarget() {
+}
+
+/// InitializeNativeTarget - The main program should call this function to
+/// initialize the native target corresponding to the host.  This is useful
+/// for JIT applications to ensure that the target gets linked in correctly.
+///
+/// It is legal for a client to make multiple calls to this function.
+inline bool InitializeNativeTarget() {
   // If we have a native target, initialize it to ensure it is linked in.
 #ifdef LLVM_NATIVE_TARGET
-    LLVM_NATIVE_TARGETINFO();
-    LLVM_NATIVE_TARGET();
-    LLVM_NATIVE_TARGETMC();
-    return false;
+  LLVM_NATIVE_TARGETINFO();
+  LLVM_NATIVE_TARGET();
+  LLVM_NATIVE_TARGETMC();
+  return false;
 #else
-    return true;
+  return true;
 #endif
-  }
+}
 
-  /// InitializeNativeTargetAsmPrinter - The main program should call
-  /// this function to initialize the native target asm printer.
-  inline bool InitializeNativeTargetAsmPrinter() {
+/// InitializeNativeTargetAsmPrinter - The main program should call
+/// this function to initialize the native target asm printer.
+inline bool InitializeNativeTargetAsmPrinter() {
   // If we have a native target, initialize the corresponding asm printer.
 #ifdef LLVM_NATIVE_ASMPRINTER
-    LLVM_NATIVE_ASMPRINTER();
-    return false;
+  LLVM_NATIVE_ASMPRINTER();
+  return false;
 #else
-    return true;
+  return true;
 #endif
-  }
+}
 
-  /// InitializeNativeTargetAsmParser - The main program should call
-  /// this function to initialize the native target asm parser.
-  inline bool InitializeNativeTargetAsmParser() {
+/// InitializeNativeTargetAsmParser - The main program should call
+/// this function to initialize the native target asm parser.
+inline bool InitializeNativeTargetAsmParser() {
   // If we have a native target, initialize the corresponding asm parser.
 #ifdef LLVM_NATIVE_ASMPARSER
-    LLVM_NATIVE_ASMPARSER();
-    return false;
+  LLVM_NATIVE_ASMPARSER();
+  return false;
 #else
-    return true;
+  return true;
 #endif
-  }
+}
 
-  /// InitializeNativeTargetDisassembler - The main program should call
-  /// this function to initialize the native target disassembler.
-  inline bool InitializeNativeTargetDisassembler() {
+/// InitializeNativeTargetDisassembler - The main program should call
+/// this function to initialize the native target disassembler.
+inline bool InitializeNativeTargetDisassembler() {
   // If we have a native target, initialize the corresponding disassembler.
 #ifdef LLVM_NATIVE_DISASSEMBLER
-    LLVM_NATIVE_DISASSEMBLER();
-    return false;
+  LLVM_NATIVE_DISASSEMBLER();
+  return false;
 #else
-    return true;
+  return true;
 #endif
-  }
+}
 
-  /// InitializeAllTargetMCAs - The main program should call
-  /// this function to initialize the target CustomBehaviour and
-  /// InstrPostProcess classes.
-  inline void InitializeAllTargetMCAs() {
+/// InitializeAllTargetMCAs - The main program should call
+/// this function to initialize the target CustomBehaviour and
+/// InstrPostProcess classes.
+inline void InitializeAllTargetMCAs() {
 #define LLVM_TARGETMCA(TargetName) LLVMInitialize##TargetName##TargetMCA();
 #include "llvm/Config/TargetMCAs.def"
-  }
 }
+} // namespace llvm
 
 #endif
diff --git a/test.cpp b/test.cpp
new file mode 100644
index 00000000000000..3d706294134f32
--- /dev/null
+++ b/test.cpp
@@ -0,0 +1 @@
+int f() { return 1; }

``````````

</details>


https://github.com/llvm/llvm-project/pull/103046


More information about the cfe-commits mailing list