[llvm-bugs] [Bug 35903] New: Incorrect formatting with C++11 attributes

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Jan 11 02:58:58 PST 2018


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

            Bug ID: 35903
           Summary: Incorrect formatting with C++11 attributes
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: Formatter
          Assignee: unassignedclangbugs at nondot.org
          Reporter: artemiev.mikhail at gmail.com
                CC: djasper at google.com, klimek at google.com,
                    llvm-bugs at lists.llvm.org

Consider the following test file:

$ cat ~/tmp/1.cpp
// attribute before entity - space inside brackets
[[ noreturn ]] void f1() {
   int a = 42;
}

// attribute before entity - no space inside brackets
[[noreturn]] void f2() {
   int a = 42;
}

// attribute after entity - space inside brackets
void f3 [[ noreturn ]] () {
   int a = 42;
}

// attribute after entity - no space inside brackets
void f4 [[noreturn]] () {
   int a = 42;
}

int main() {
   // attribute after entity - space inside brackets
   int a [[ gnu::unused ]] = 0;
   // attribute after entity - no space inside brackets
   int b [[gnu::unused]] = 1;
   // attribute before entity - space inside brackets
   [[ gnu::unused ]] int c = 2;
   // attribute before entity - no space inside brackets
   [[gnu::unused]] int d = 3;
   return 0;
}

Formatting it with the default LLVM style produces the following:

$ bin/clang-format --style=llvm ~/tmp/1.cpp
// attribute before entity - space inside brackets
[[noreturn]] void f1() { int a = 42; }

    // attribute before entity - no space inside brackets
    [[noreturn]] void f2() {
  int a = 42;
}

// attribute after entity - space inside brackets
void f3[[noreturn]]() { int a = 42; }

// attribute after entity - no space inside brackets
void f4[[noreturn]]() { int a = 42; }

int main() {
  // attribute after entity - space inside brackets
  int a[[gnu::unused]] = 0;
  // attribute after entity - no space inside brackets
  int b[[gnu::unused]] = 1;
  // attribute before entity - space inside brackets
  [[gnu::unused]] int c = 2;
  // attribute before entity - no space inside brackets
  [[gnu::unused]] int d = 3;
  return 0;
}

Issues:
1. When an attribute is placed after an entity, the formatter removes the space
between the identifier and the attribute.
2. Something went wrong with the f2 formatting.

With the following settings some other issues can be seen:

$ bin/clang-format --style="{BasedOnStyle: llvm, SpacesInSquareBrackets: true,
AlwaysBreakAfterReturnType: TopLevelDefinitions}" ~/tmp/1.cpp
// attribute before entity - space inside brackets
[[noreturn]] void
f1() { int a = 42; }

    // attribute before entity - no space inside brackets
    [ [noreturn] ] void f2() {
  int a = 42;
}

// attribute after entity - space inside brackets
void f3[ [noreturn] ]() { int a = 42; }

// attribute after entity - no space inside brackets
void f4[ [noreturn] ]() { int a = 42; }

int
main() {
  // attribute after entity - space inside brackets
  int a[ [gnu::unused] ] = 0;
  // attribute after entity - no space inside brackets
  int b[ [gnu::unused] ] = 1;
  // attribute before entity - space inside brackets
  [[gnu::unused]] int c = 2;
  // attribute before entity - no space inside brackets
  [[gnu::unused]] int d = 3;
  return 0;
}

Issues:
3. With 'SpacesInSquareBrackets: true', the attribute becomes '[ [attribute] ]'
4. When an attribute is placed after an entity, the
'AlwaysBreakAfterReturnType' has no effect (cf. functions f3, f4)

This test file was tried on a freshly built clang from trunk.

-- 
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/20180111/485f5ebf/attachment.html>


More information about the llvm-bugs mailing list