llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

clang-format misplaces `*` in declaration of pointer to struct

Open fanf2 opened this issue 4 years ago • 9 comments

Our code in BIND is auto-formatted with clang-format using a .clang-format configuration that includes PointerAlignment: Right and PointerBindsToType: false

When I run the following on the command line:

$ echo 'struct { int foo; } *foop;' | clang-format
struct {
	int foo;
} * foop;

I get a space before foop, where I expected the last line to be } *foop;

fanf2 avatar Jun 01 '22 12:06 fanf2

@llvm/issue-subscribers-clang-format

llvmbot avatar Jun 01 '22 13:06 llvmbot

PointerBindsToType I'm not sure thats a current option

Having said that I kind of expect it to work with just PointerAlignment, (but I see it doesn't)

struct {
  int foo;
} * foop;

BasedOnStyle: LLVM
PointerAlignment: Right

mydeveloperday avatar Jun 11 '22 16:06 mydeveloperday

@llvm/issue-subscribers-bug

llvmbot avatar Jun 11 '22 16:06 llvmbot

@llvm/issue-subscribers-good-first-issue

llvmbot avatar Jun 11 '22 16:06 llvmbot

The problem is that determineStarAmpUsage categorizes the star token as a binary operator. In my opinion, we can remove the tok::r_brace condition because star tokens will not be binary operators when they are behind right brackets in C and C++.

    if (PrevToken->Tok.isLiteral() ||
        PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
                           tok::kw_false, tok::r_brace)) {
      return TT_BinaryOperator;
    }

By the way, I want to add new test cases in FormatTest.cpp. But I am a newbie in LLVM and don't know how to run unit tests. Could somebody tell me? Thanks.

jackhong12 avatar Jun 15 '22 08:06 jackhong12

I build on windows (in a visual studio shell), and I do this (one time)

mkdir build
cd build
export CC=cl.exe
export CXX=cl.exe
c:/Program\ Files/CMake/bin/cmake.exe ../llvm-project/llvm -G "Ninja" \
-DLLVM_BUILD_TESTS=ON \
-DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
-DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra"

Then I do this in the build directory

ninja clang-format
ninja FormatTests

./tools/clang/unittests/Format/FormatTests.exe

mydeveloperday avatar Jun 15 '22 13:06 mydeveloperday

we can remove the tok::r_brace condition because star tokens

My assumption would be that this will break one of the existing unit tests... you won't know until you try..

are you saying ... } * .... won't be a binary operator? that is true but this is &,&& and * so you have to consider

} & and } && and that can occur.. you will soon discover this use case

 template <class T,
           class = typename ::std::enable_if<
              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>

But you may be able to handle that case by something like:

     if (PrevToken->Tok.isLiteral() ||
         PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
-                           tok::kw_false, tok::r_brace))
+                           tok::kw_false))
+      return TT_BinaryOperator;
+
+    if (PrevToken->is(tok::r_brace) && Tok.isOneOf(tok::amp, tok::ampamp))
       return TT_BinaryOperator;

     const FormatToken *NextNonParen = NextToken;

mydeveloperday avatar Jun 15 '22 13:06 mydeveloperday

@mydeveloperday You are right. Thanks a lot.

jackhong12 avatar Jun 15 '22 16:06 jackhong12

Revision: https://reviews.llvm.org/D127873

jackhong12 avatar Jun 15 '22 16:06 jackhong12