llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.24k stars 11.66k forks source link

[clang-format] AlignTrailingComments on PP dependent on newline? #54525

Open chilippso opened 2 years ago

chilippso commented 2 years ago

clang-format Version: 13.0. Description: AlignTrailingComments (?) behaves different for the following two input cases

Reproduction Steps: Format the following two blocks of input code (no whitespace at all before comments) with default settings (no .clang-format specified). Notice the different aligned trailing comment on #else with #endif when there is a newline at the end of the class definition.

Input without newline at end of class definition:

#define HAS_CPP_11 0

class SomeClass
{
 protected:
#if HAS_CPP_11// C++11 support
  SomeClass(const NonCopyableClass&) = delete;
  SomeClass& operator=(const NonCopyableClass&) = delete;
#else// HAS_CPP_11
 private:
  SomeClass(const SomeClass&);
  SomeClass& operator=(const SomeClass&);
#endif// HAS_CPP_11
};

Output without newline (comment on else is aligned with endif)

#define HAS_CPP_11 0

class SomeClass
{
 protected:
#if HAS_CPP_11 // C++11 support
  SomeClass(const NonCopyableClass&) = delete;
  SomeClass& operator=(const NonCopyableClass&) = delete;
#else  // HAS_CPP_11
 private:
  SomeClass(const SomeClass&);
  SomeClass& operator=(const SomeClass&);
#endif // HAS_CPP_11
};

Input with newline at end of class definition:

#define HAS_CPP_11 0

class SomeClass
{
 protected:
#if HAS_CPP_11// C++11 support
  SomeClass(const NonCopyableClass&) = delete;
  SomeClass& operator=(const NonCopyableClass&) = delete;
#else// HAS_CPP_11
 private:
  SomeClass(const SomeClass&);
  SomeClass& operator=(const SomeClass&);

#endif// HAS_CPP_11
};

Output with newline (comment on else is not aligned with endif)

#define HAS_CPP_11 0

class SomeClass
{
 protected:
#if HAS_CPP_11 // C++11 support
  SomeClass(const NonCopyableClass&) = delete;
  SomeClass& operator=(const NonCopyableClass&) = delete;
#else // HAS_CPP_11
 private:
  SomeClass(const SomeClass&);
  SomeClass& operator=(const SomeClass&);

#endif // HAS_CPP_11
};

I am curious why this is the case and also why it is not aligned with the initial if-PP. Also I would like to know how to set clang-format to not differentiate and either always align or not, regardless of a newline at the end of the definition (in this case).

llvmbot commented 2 years ago

@llvm/issue-subscribers-clang-format