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

[clangd] macro definition causes clangd to assert

Open vabridgers opened this issue 6 years ago • 6 comments

Bugzilla Link 45428
Resolution FIXED
Resolved on Apr 07, 2020 06:08
Version trunk
OS Linux
CC @sam-mccall
Fixed by commit(s) d66afd6dde542dc373f87e07fe764c071fe20d76

Extended Description

I came across a case in proprietary source code that causes clangd to assert, and distilled the problem into a a very simple test case that can be applied to clang/unittests/Tooling/Syntax/TokensTest.cpp.

I bisected the source tree, and the assert started appearing at git sha "5e69f27ef7086287519d0c04192108d322bd6e82 - [Syntax] Do not glue multiple empty PP expansions to a single mapping".

There is a comment in that patch that appears suspicious ...

clang/include/clang/Tooling/Syntax/Tokens.h ~L160: +/// FIXME: allow mappings into macro arguments.

Maybe related?

Ilya, could you have a look at this when you get a chance?

Thanks - Vince

The macro test case is:

#define N 42 #define M2(a1,...) {VA_ARGS;} #define M1(a2,...) M2(a2,VA_ARGS)) #define M0 M1 M0(0,N);

Simple patch ... diff --git a/clang/unittests/Tooling/Syntax/TokensTest.cpp b/clang/unittests/Tooling/Syntax/TokensTest.cpp index 256096d..e924dea 100644 --- a/clang/unittests/Tooling/Syntax/TokensTest.cpp +++ b/clang/unittests/Tooling/Syntax/TokensTest.cpp @@ -484,6 +484,21 @@ file './input.cpp' ['EMPTY'_9, 'EMPTY_FUNC'_10) => [''_0, ''_0) ['EMPTY_FUNC'_10, ''_18) => [''_0, ''_0) )"},

  •  // New issue: Should not crash.
    
  •  // Verification portion of this TBD
    
  •  {R"cpp(
    
  • #define N 42
  • #define M2(a1,...) {VA_ARGS;}
  • #define M1(a2,...) M2(a2,VA_ARGS))
  • #define M0 M1
  • M0(0,N);
  • )cpp",
  •   R"(expanded tokens:
    

+file './input.cpp'

  • spelled tokens:
  • mappings: +)"}, // File ends with a macro replacement. {R"cpp( #define FOO 10+10;

Obfuscated debug output and assertion message...

Trying /repo/einvbri/clangd-repro/.clang-format... Trying /repo/einvbri/clangd-repro/_clang-format... Trying /repo/einvbri/.clang-format... Trying /repo/einvbri/_clang-format... Trying /repo/.clang-format... Trying /repo/_clang-format... Trying /.clang-format... Trying /_clang-format... Token: Token(void, void, length = 4) Token: Token(foo, identifier, length = 3) Token: Token((, l_paren, length = 1) Token: Token(void, void, length = 4) Token: Token(), r_paren, length = 1) Token: Token({, l_brace, length = 1) Token: Token({, l_brace, length = 1) Token: Token(42, numeric_constant, length = 2) Token: Token(;, semi, length = 1) Token: Token(}, r_brace, length = 1) Token: Token(), r_paren, length = 1) Token: Token(;, semi, length = 1) Token: Token(}, r_brace, length = 1) Token: Token(``, eof, length = 0) clangd: /llvm/clang/lib/Tooling/Syntax/Tokens.cpp:620: void clang::syntax::TokenCollector::Builder::consumeMapping(clang::syntax::TokenBuffer::MarkedFile&, unsigned int, unsigned int, unsigned int, unsigned int&): Assertion `!HitMapping && "recursive macro expansion?"' failed.

vabridgers avatar Apr 04 '20 10:04 vabridgers

Thanks again for tracking this down! This is fixed at head.

sam-mccall avatar Apr 07 '20 13:04 sam-mccall

Abandoning https://reviews.llvm.org/D77507, thanks for the fix.

vabridgers avatar Apr 07 '20 12:04 vabridgers

There is some conceptual looseness/assumptions in the idea of a "top-level expansion" that are being uncovered by this testcase.

https://reviews.llvm.org/D77614 refactors TokenCollector::Builder a bit to allow this to be fixed in one place in https://reviews.llvm.org/D77615.

sam-mccall avatar Apr 07 '20 09:04 sam-mccall

https://reviews.llvm.org/D77507

vabridgers avatar Apr 05 '20 18:04 vabridgers

I simplified the case a little more, this case exposes the problem (same assertion):

  {R"cpp(
#define N 42 
#define M1(a1) foo(a1)
#define M0  M1
M0(N);
)cpp",
   R"(expanded tokens:

foo ( 42 ) ; file './input.cpp' spelled tokens: # define N 42 # define M0 ( a1 ) foo ( a1 ) M0 ( N ) ; mappings: ['#'_0, 'M0'_14) => ['foo'_0, 'foo'_0) ['M0'_14, ';'_18) => ['foo'_0, ';'_4) )"},

This one works.

  {R"cpp(
#define N 42 
#define M0(a1) foo(a1)
M0(N);
)cpp",
   R"(expanded tokens:

foo ( 42 ) ; file './input.cpp' spelled tokens: # define N 42 # define M0 ( a1 ) foo ( a1 ) M0 ( N ) ; mappings: ['#'_0, 'M0'_14) => ['foo'_0, 'foo'_0) ['M0'_14, ';'_18) => ['foo'_0, ';'_4) )"},

vabridgers avatar Apr 04 '20 22:04 vabridgers

assigned to @sam-mccall

vabridgers avatar Apr 04 '20 10:04 vabridgers