Issue 233300
Summary [clang-format] Macros expansion after #if/#else triggers FormatToken::setType assertion
Labels clang-format
Assignees
Reporter andremueiot
    clang-format crashes when a declaration using a formatter-expanded attribute
macro follows a member function containing an `#if`/`#else`. The declaration
also has an annotation macro that is not listed in `Macros`; both annotations
receive `*this`.

## Version and platform

Windows, using:

```text
clang-format version 24.0.0git (https://github.com/llvm/llvm-project.git 4bd435d59f89ded8e537e5c6e0b560ad89518c3f)
```

## Reproducer

Save these two files in the same directory.

`repro.cpp`:

```cpp
// Copyright (c) Microsoft Corporation. All rights reserved.
#define UNKNOWN(x)
#define ATTRIBUTE(x)
class Example
{
    void First()
    {
#if 1
#else
#endif
    }
    UNKNOWN(*this) ATTRIBUTE(*this)
    void Second();
};
```

`.clang-format`:

```yaml
Macros:
  - 'ATTRIBUTE(x)=[[clang::annotate(x)]]'
```

Run:

```text
clang-format --style=file repro.cpp
```

No headers, project dependencies, or `-i` are needed.

## Expected result

The formatter should not crash. The source is valid C++: both source-level
annotation macros expand to nothing.

## Actual result

```text
Assertion failed: (!TypeIsFinalized || T == Type) && "Please use overwriteFixedType to change a fixed type.", file E:\github\llvm-project\clang\lib\Format\FormatToken.h, line 470
Exception Code: 0x80000003
```

Full crash output is supplied separately as `crash.txt`.

## Additional observations

Each of the following independently prevents the crash:

- Remove the `#else` from `First`.
- Change both annotation arguments from `*this` to `this`.
- Remove `UNKNOWN(*this)`.
- Add `UNKNOWN(x)=[[clang::annotate(x)]]` to `Macros`.
- Remove the `Macros` setting and use the default style.

These are observations from the reduced reproducer, not a confirmed internal
root cause. Other reports have the same assertion text, but I have not
established that this is the same defect.

**tested version**: clang-format version 24.0.0git
https://github.com/llvm/llvm-project.git
4bd435d59f89ded8e537e5c6e0b560ad89518c3f
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to