| Issue |
184008
|
| Summary |
AlignTrailingComments fails to align comments across preprocessor conditionals
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
RahulSisondia
|
### Summary
When `AlignTrailingComments: true` is enabled, clang-format correctly aligns trailing comments in function parameters without preprocessor directives, but fails to maintain this alignment when `#if/#else/#endif` blocks are introduced. The alignment algorithm treats each preprocessor branch as a separate scope, resulting in inconsistent comment positioning.
### Environment
- clang-format version: 21.1.5
- Operating System: macOS
### Configuration (.clang-format)
```yaml
Language: Cpp
SpacesBeforeTrailingComments: 2
AlignTrailingComments: true
```
### Test Case
```cpp
// Test 1: Without preprocessor - comments align correctly
static bool test1(
dict_index_t *index, /*!< in: index */
const trx_t *trx) /*!< in: transaction or NULL */
{
return true;
}
// Test 2: With preprocessor - alignment breaks
static bool test2(
dict_index_t *index, /*!< in: index */
#if FEATURE_ENABLED
const row_prebuilt_t *prebuilt) /*!< in: prebuilt or NULL */
#else
const trx_t *trx) /*!< in: transaction or NULL */
#endif
{
return true;
}
```
### Expected Behavior
Comments should be aligned to the same column across all parameters, including those inside preprocessor conditionals, just like they are without preprocessor directives.
**Test 1 output (correct):**
```cpp
static bool test1(
dict_index_t *index, /*!< in: index */
const trx_t *trx) /*!< in: transaction or NULL */
```
- Line 2: `dict_index_t *index,` (24 chars) + 1 space → comment at column 25
- Line 3: `const trx_t *trx)` (21 chars) + 4 spaces → comment at column 25
- ✅ Both comments aligned at column 25
**Test 2 expected output:**
```cpp
static bool test2(
dict_index_t *index, /*!< in: index */
#if FEATURE_ENABLED
const row_prebuilt_t *prebuilt) /*!< in: prebuilt or NULL */
#else
const trx_t *trx) /*!< in: transaction or NULL */
#endif
```
- Line 2: `dict_index_t *index,` (24 chars) + minimum 2 spaces → comment should start at column 37 (based on longest line)
- Line 4: `const row_prebuilt_t *prebuilt)` (35 chars) + 2 spaces → comment at column 37
- Line 6: `const trx_t *trx)` (21 chars) + 16 spaces → comment at column 37
- All three comments should align to the same column
### Actual Behavior
Clang-format treats each preprocessor branch independently and fails to align comments across the entire function signature.
**Test 2 actual output:**
```cpp
static bool test2(
dict_index_t *index, /*!< in: index */
#if FEATURE_ENABLED
const row_prebuilt_t *prebuilt) /*!< in: prebuilt or NULL */
#else
const trx_t *trx) /*!< in: transaction or NULL */
#endif
```
- Line 2: comment at column 25 (aligned with first parameter)
- Line 4: comment at column 36 (only 1 space, not aligned)
- Line 6: comment at column 22 (only 1 space, not aligned)
- ❌ Comments are NOT aligned across preprocessor branches
### Root Cause
The `AlignTrailingComments` algorithm treats `#if`, `#else`, and `#endif` as scope boundaries and processes each branch independently, rather than considering all parameters together as a single alignment group.
### Reproduction Steps
1. Save the test case to a file (e.g., `test.cpp`)
2. Create a `.clang-format` file with the configuration above
3. Run: `clang-format test.cpp`
4. Observe that Test 1 has properly aligned comments, but Test 2 does not
### Workaround
Using `AlignTrailingComments: { Kind: Leave }` preserves the original spacing but disables all alignment functionality.
### Additional Context
This issue significantly affects codebases that use preprocessor conditionals for platform-specific or feature-specific code variations (e.g., different function signatures for different platforms). The inconsistent formatting makes the code harder to read and maintain.
### Suggested Fix
The alignment algorithm should either:
1. **Preferred**: Align trailing comments across all preprocessor branches as if they were part of the same scope, OR
2. **Acceptable**: Apply consistent `SpacesBeforeTrailingComments` to all lines when alignment across branches is not possible
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs