[llvm-bugs] [Bug 28616] New: Fix ordering of function attributes under MSVCFormatting policy in DeclPrinter

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Jul 19 08:34:31 PDT 2016


https://llvm.org/bugs/show_bug.cgi?id=28616

            Bug ID: 28616
           Summary: Fix ordering of function attributes under
                    MSVCFormatting policy in DeclPrinter
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: keyboardsmoke at gmail.com
                CC: llvm-bugs at lists.llvm.org
    Classification: Unclassified

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";

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20160719/d8464388/attachment.html>


More information about the llvm-bugs mailing list