<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Fix ordering of function attributes under MSVCFormatting policy in DeclPrinter"
   href="https://llvm.org/bugs/show_bug.cgi?id=28616">28616</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Fix ordering of function attributes under MSVCFormatting policy in DeclPrinter
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>-New Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>keyboardsmoke@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>This fix would place the attributes at the beginning of a function (but after
specifiers).
Ideally, I'd like them to be after the return type is declared, but this is
acceptable by MSVC standards.

I am not intimately familiar with clang's rules regarding just jamming in space
characters like that, but since other strings are being appended to out in a
similar fashion I thought it would be acceptable.

The reason for this patch is: clang's generated functions do not compile under
MSVC, as they are invalid.

Take this function:
__forceinline __declspec(noalias, deprecated("none")) void bodyFunction() {
    printf("Hello, world!\n");
}

Under trunk DeclPrinter would output this function as:
void bodyFunction() __forceinline __declspec(noalias, deprecated("none")) {
    printf("Hello, world!\n");
}

Which would result in a compilation error if you were to try to utilize it in a
project.

This patch allows (when MSVCFormatting is enabled) functions printed by
DeclPrinter to be compiled under MSVC, by forcing attributes to be printed
before the function declaration, which is acceptable under MSVC.

inline  __declspec(deprecated("none")) __declspec(noalias) __forceinline void
bodyFunction() {
    printf("Hello, world!\n");
}

(The duplicate inline/__forceinline is for another submission)

Index: DeclPrinter.cpp
===================================================================
--- DeclPrinter.cpp    (revision 275612)
+++ DeclPrinter.cpp    (working copy)
@@ -474,6 +474,13 @@
   SubPolicy.SuppressSpecifiers = false;
   std::string Proto = D->getNameInfo().getAsString();

+  if (Policy.MSVCFormatting) {
+    prettyPrintAttributes(D);
+
+    if (D->attr_begin() != D->attr_end())
+      Out << " ";
+  }
+
   QualType Ty = D->getType();
   while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
     Proto = '(' + Proto + ')';
@@ -628,7 +635,8 @@
     Ty.print(Out, Policy, Proto);
   }

-  prettyPrintAttributes(D);
+  if (!Policy.MSVCFormatting)
+    prettyPrintAttributes(D);

   if (D->isPure())
     Out << " = 0";</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>