<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/61409>61409</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Incorrect order of predefined macros in DWARF
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang,
            wrong-debug,
            debuginfo
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          dwblaikie
      </td>
    </tr>
</table>

<pre>
    `clang -fdebug-macro -g` places predefined and command-line defined macros after the first macro file description (& any macros contained in that file), rather than before.

This is because Clang's macro callback API considers these predefined macros as part of the file rather than before it, and the callbacks come in this order:
```
// This is the expected flow of enter/exit compiler and user files:
// - Main File Enter
//   - <built-in> file enter
//     {Compiler macro definitions} - (Line=0, no scope)
//     - (Optional) <command line> file enter
//     {Command line macro definitions} - (Line=0, no scope)
//     - (Optional) <command line> file exit
//     {Command line file includes} - (Line=0, Main file scope)
//       {macro definitions and file includes} - (Line!=0, Parent scope)
//   - <built-in> file exit
//   {User code macro definitions and file includes} - (Line!=0, Parent scope)
```
(according to [MacroPPCallbacks.cpp](https://github.com/llvm/llvm-project/blob/95375ffa0b45c00e41fa561b9eef5eb8e09ff7ab/clang/lib/CodeGen/MacroPPCallbacks.cpp#L64-L74))

But DWARF and the LLVM IR debug info metadata that's modeled on DWARF consider the predefined/command line defined macros to be outside the main file.

So what happens is the main file is created (as a child of the null file) in the DI IR metadata via IRBuilder, then the predefined macros are created also as children of the null file - and end up going after the main file.

This patch fixes the problem, but isn't shippable because it makes LLVM's APIs subtly dependent on this Clang quirk:
```
diff --git a/llvm/lib/IR/DIBuilder.cpp b/llvm/lib/IR/DIBuilder.cpp
index eacfa475a1a3..93555cf69ce2 100644
--- a/llvm/lib/IR/DIBuilder.cpp
+++ b/llvm/lib/IR/DIBuilder.cpp
@@ -237,7 +237,13 @@ DIMacro *DIBuilder::createMacro(DIMacroFile *Parent, unsigned LineNumber,
 MacroType == dwarf::DW_MACINFO_define) &&
          "Unexpected macro type");
   auto *M = DIMacro::get(VMContext, MacroType, LineNumber, Name, Value);
-  AllMacrosPerParent[Parent].insert(M);
+  auto &PP = AllMacrosPerParent[Parent];
+  if (!Parent) {
+    auto E = PP.pop_back_val();
+    PP.insert(M);
+    PP.insert(E);
+  }
+ PP.insert(M);
   return M;
 }
 ```
 
 The LLVM API could take some parameter to specify this behavior rather than this hardcoded version, but even that'd be a bit weird.
 
 Alternatively the Clang code could defer creating the main file until after the predefined macros are done - like waiting until it sees an attempt to define a macro in the main file - and only then would it create the main file macro description.
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8V1uP4joS_jXmpRQUnISQBx64DCuk7jlods6cx5ETV8Dbxs7aTtP971e2E5q-zZnVSttCDbHr8pWr_FWFWSuOCnFJijUpthPWu5M2S36pJRMPAie15s9LMk8bydQRkpZj3R-TM2uMhuRI5il0kjVooTPIsRUKOTDFodHnM1M8kUIhjBtBzQJrHRpwJ4RWGOviMrRCeknbGNE5oRUQuiB0Dkw9j4qNVo4FS0KBOzEXlAitCN2AYe4UzDIFNbba4JSkW5Ku4v_vJ2FBWKixYb1F2PiACC3t4L5hUtaseYDVYe8dWcHRWI_S4m1wYwwWOmYc6HYIROIHCEA4D80fiJcaffhIzhiDEBa04WhINgAl83T4xEe6I3QHI3xvBp86bBxyaKW-eASoHBpCd_gknDfdCYkmeO0tmgDOvtiPBhO4Z0LBzgP_EvRvdwESINmm7oV0iVAk-xJDxA8kAUi53oxe42mG0xI-jZaUW2-MLu6EQpJtU38iSoNtdBdy99ZaEP4j1ACThFYeyFBOIION3wBzlf6_AnoS7m_xBEmhGtlz_BhLSEwQ-xRSMPsuspDyX9ins9HFgRlU7jP7n-T-fXSkXP_pK6zR_IOD_l_hvL0IC9Y02nChjuA0kGJ97z0eDpvxWk2briPFltDFybkulHxAehTu1NfTRp8J3Un5OH4lndH_wsYRuqulrgndVUV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