llvm / llvm/llvm-project

[Clang] 'import' followed by '<<', '<=', '<<=' or '<=>' wrongly lexed as a module header-name

Open
#210,935 6 comments 0 reactions 0 assignees View on GitHub
clang:frontend clang:modules
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Clang's `Preprocessor::HandleModuleContextualKeyword` enables ParsingFilename before peeking the token after import, forcing the lexer to scan any leading < as an angled header-name. But per [cpp.pre]p2 a module directive is introduced only when import is followed by a header-name, <, identifier, or :. A compound punctuator that only starts with < (<<, <=, <<=, <=>) is a single preprocessing-token and none of those alternatives, yet clang misparses it:

`import << 1; `— import is misclassified as a header-unit-import (the angled scan hits no > and falls back to a synthetic bare <).
`import << (a > b); / import << ` — a later > makes the scan fold the text between into a bogus header-name → spurious "file not found".
`import <=>; `— reports '=' file not found.

1. Minimal <=> reproducer — [godbolt.org/z/5de1z9sr7](https://godbolt.org/z/5de1z9sr7)

```c++
// clang -std=c++20 -fsyntax-only
export module M;
import <=>; // error: '=' file not found ← should not lex '<=>' as a header-name
```

2. << shift cases — [godbolt.org/z/dKdfcsGMq](https://godbolt.org/z/dKdfcsGMq)

```c++
int import = 0;
int d(int x) { return
import << (x > 0); // valid shift → clang: "'...' file not found"
}
int a() { return
import << 1; // import' silently misclassified as header-unit-import
}
```

Contributor guide

Open the contributing guide

Research direction

Start at Clang's Preprocessor::HandleModuleContextualKeyword and reproduce the issue with clang -std=c++20 -fsyntax-only using the provided <<= and << examples. Trace how the token after import is peeked and verify that compound punctuators are not treated as header-names; done means the bogus header-name and file-not-found diagnostics no longer occur.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.