[llvm-bugs] [Bug 45428] New: [clangd] macro definition causes clangd to assert

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Apr 4 03:38:57 PDT 2020


https://bugs.llvm.org/show_bug.cgi?id=45428

            Bug ID: 45428
           Summary: [clangd] macro definition causes clangd to assert
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: Tooling
          Assignee: unassignedclangbugs at nondot.org
          Reporter: vince.a.bridgers at gmail.com
                CC: llvm-bugs at lists.llvm.org

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) => ['<eof>'_0, '<eof>'_0)
     ['EMPTY_FUNC'_10, '<eof>'_18) => ['<eof>'_0, '<eof>'_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:
+  <empty>
+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: <root>/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.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20200404/a03436b9/attachment.html>


More information about the llvm-bugs mailing list