llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.12k stars 12.01k forks source link

clang assertion since r167353 #14771

Closed llvmbot closed 11 years ago

llvmbot commented 11 years ago
Bugzilla Link 14399
Resolution FIXED
Resolved on Nov 30, 2012 00:19
Version trunk
OS Linux
Reporter LLVM Bugzilla Contributor
CC @ahatanak,@d0k,@efriedma-quic,@zygoloid

Extended Description

Typescript for reproducing the problem:

$ cat bar.c void foo_aa() {

define / / BARC(c, / /b, a, ...) (a+b+/ /c + __VA_ARGS__ +0)

iequals(__LINE__, BARC(4,3,2,6,8), 8);

} $ clang -c bar.c -emit-llvm bar.c:4:2: warning: implicit declaration of function 'iequals' is invalid in C99 [-Wimplicit-function-declaration] iequals(LINE, BARC(4,3,2,6,8), 8); ^ bar.c:4:20: warning: expression result unused [-Wunused-value] clang: /scratch/bb-slaves/mipssw005/LLVM_optimize_install/source/tools/clang/lib/Frontend/TextDiagnostic.cpp:1086: void clang::TextDiagnostic::highlightRange(const clang::CharSourceRange&, unsigned int, clang::FileID, const SourceColumnMap&, std::string&, const clang::SourceManager&): Assertion `StartColNo <= EndColNo && "Invalid range!"' failed. 0 clang 0x00000000020b701f 1 clang 0x00000000020b8f9a 2 libpthread.so.0 0x00007f8dd46a88f0 3 libc.so.6 0x00007f8dd3997a75 gsignal + 53 4 libc.so.6 0x00007f8dd399b5c0 abort + 384 5 libc.so.6 0x00007f8dd3990941 assert_fail + 241 6 clang 0x000000000071c774 clang::TextDiagnostic::highlightRange(clang::CharSourceRange const&, unsigned int, clang::FileID, SourceColumnMap const&, std::string&, clang::SourceManager const&) + 1748 7 clang 0x000000000071daea clang::TextDiagnostic::emitSnippetAndCaret(clang::SourceLocation, clang::DiagnosticsEngine::Level, llvm::SmallVectorImpl&, llvm::ArrayRef, clang::SourceManager const&) + 2394 8 clang 0x0000000000712893 clang::DiagnosticRenderer::emitMacroExpansionsAndCarets(clang::SourceLocation, clang::DiagnosticsEngine::Level, llvm::SmallVectorImpl&, llvm::ArrayRef, clang::SourceManager const&, unsigned int&, unsigned int) + 211 9 clang 0x0000000000712ee6 clang::DiagnosticRenderer::emitMacroExpansionsAndCarets(clang::SourceLocation, clang::DiagnosticsEngine::Level, llvm::SmallVectorImpl&, llvm::ArrayRef, clang::SourceManager const&, unsigned int&, unsigned int) + 1830 10 clang 0x0000000000713637 clang::DiagnosticRenderer::emitDiagnostic(clang::SourceLocation, clang::DiagnosticsEngine::Level, llvm::StringRef, llvm::ArrayRef, llvm::ArrayRef, clang::SourceManager const, llvm::PointerUnion<clang::Diagnostic const, clang::StoredDiagnostic const>) + 983 11 clang 0x00000000006dba51 clang::TextDiagnosticPrinter::HandleDiagnostic(clang::DiagnosticsEngine::Level, clang::Diagnostic const&) + 561 12 clang 0x00000000013da8a4 clang::DiagnosticIDs::EmitDiag(clang::DiagnosticsEngine&, clang::DiagnosticIDs::Level) const + 52 13 clang 0x00000000013dcbde clang::DiagnosticIDs::ProcessDiag(clang::DiagnosticsEngine&) const + 462 14 clang 0x00000000013d64e4 clang::DiagnosticsEngine::EmitCurrentDiagnostic(bool) + 36 15 clang 0x0000000000a3e99e clang::Sema::EmitCurrentDiagnostic(unsigned int) + 862 16 clang 0x0000000000a4700f 17 clang 0x0000000000b67d0d clang::Sema::DiagRuntimeBehavior(clang::SourceLocation, clang::Stmt const, clang::PartialDiagnostic const&) + 1565 18 clang 0x0000000000c789e8 clang::Sema::DiagnoseUnusedExprResult(clang::Stmt const) + 456 19 clang 0x0000000000b8aafe clang::Sema::CreateBuiltinBinOp(clang::SourceLocation, clang::BinaryOperatorKind, clang::Expr, clang::Expr) + 2862 20 clang 0x0000000000b8aed3 clang::Sema::BuildBinOp(clang::Scope, clang::SourceLocation, clang::BinaryOperatorKind, clang::Expr, clang::Expr) + 259 21 clang 0x0000000000b8b286 clang::Sema::ActOnBinOp(clang::Scope, clang::SourceLocation, clang::tok::TokenKind, clang::Expr, clang::Expr) + 278 22 clang 0x00000000009e7dc0 clang::Parser::ParseRHSOfBinaryExpression(clang::ActionResult<clang::Expr, true>, clang::prec::Level) + 2400 23 clang 0x00000000009e9349 clang::Parser::ParseExpression(clang::Parser::TypeCastState) + 25 24 clang 0x00000000009ebaa4 clang::Parser::ParseParenExpression(clang::Parser::ParenParseOption&, bool, bool, clang::OpaquePtr&, clang::SourceLocation&) + 2500 25 clang 0x00000000009e5f3a clang::Parser::ParseCastExpression(bool, bool, bool&, clang::Parser::TypeCastState) + 698 26 clang 0x00000000009e73dd clang::Parser::ParseCastExpression(bool, bool, clang::Parser::TypeCastState) + 29 27 clang 0x00000000009e8def clang::Parser::ParseAssignmentExpression(clang::Parser::TypeCastState) + 31 28 clang 0x00000000009e8ec2 clang::Parser::ParseExpressionList(llvm::SmallVectorImpl<clang::Expr>&, llvm::SmallVectorImpl&, void (clang::Sema::)(clang::Scope, clang::Expr, llvm::ArrayRef<clang::Expr>), clang::Expr) + 130 29 clang 0x00000000009e96ba clang::Parser::ParsePostfixExpressionSuffix(clang::ActionResult<clang::Expr, true>) + 874 30 clang 0x00000000009e5d78 clang::Parser::ParseCastExpression(bool, bool, bool&, clang::Parser::TypeCastState) + 248 31 clang 0x00000000009e73dd clang::Parser::ParseCastExpression(bool, bool, clang::Parser::TypeCastState) + 29 32 clang 0x00000000009e8def clang::Parser::ParseAssignmentExpression(clang::Parser::TypeCastState) + 31 33 clang 0x00000000009e9339 clang::Parser::ParseExpression(clang::Parser::TypeCastState) + 9 34 clang 0x0000000000a191e8 clang::Parser::ParseExprStatement() + 72 35 clang 0x0000000000a14605 clang::Parser::ParseStatementOrDeclarationAfterAttributes(llvm::SmallVector<clang::Stmt, 32u>&, bool, clang::SourceLocation, clang::Parser::ParsedAttributesWithRange&) + 1909 36 clang 0x0000000000a1479e clang::Parser::ParseStatementOrDeclaration(llvm::SmallVector<clang::Stmt, 32u>&, bool, clang::SourceLocation) + 142 37 clang 0x0000000000a15801 clang::Parser::ParseCompoundStatementBody(bool) + 1841 38 clang 0x0000000000a15e6a clang::Parser::ParseFunctionStatementBody(clang::Decl, clang::Parser::ParseScope&) + 154 39 clang 0x00000000009b9aa9 clang::Parser::ParseFunctionDefinition(clang::ParsingDeclarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::LateParsedAttrList) + 2617 40 clang 0x00000000009cc64d clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, unsigned int, bool, clang::SourceLocation, clang::Parser::ForRangeInit) + 3085 41 clang 0x00000000009b5099 clang::Parser::ParseDeclOrFunctionDefInternal(clang::Parser::ParsedAttributesWithRange&, clang::ParsingDeclSpec&, clang::AccessSpecifier) + 249 42 clang 0x00000000009b56c4 clang::Parser::ParseDeclarationOrFunctionDefinition(clang::Parser::ParsedAttributesWithRange&, clang::ParsingDeclSpec, clang::AccessSpecifier) + 804 43 clang 0x00000000009b70ba clang::Parser::ParseExternalDeclaration(clang::Parser::ParsedAttributesWithRange&, clang::ParsingDeclSpec) + 1674 44 clang 0x00000000009b7652 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr&) + 194 45 clang 0x00000000009ae548 clang::ParseAST(clang::Sema&, bool, bool) + 296 46 clang 0x00000000008336fe clang::CodeGenAction::ExecuteAction() + 78 47 clang 0x00000000006ba8b9 clang::FrontendAction::Execute() + 185 48 clang 0x0000000000697655 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) + 341 49 clang 0x000000000067f20c clang::ExecuteCompilerInvocation(clang::CompilerInstance) + 1708 50 clang 0x00000000006767b8 cc1_main(char const, char const, char const, void) + 1240 51 clang 0x000000000067d847 main + 7431 52 libc.so.6 0x00007f8dd3982c4d libc_start_main + 253 53 clang 0x0000000000675749 Stack dump:

  1. Program arguments: /mips/proj/build-compiler/log.2012-11-19/LLVM-clang/install-master-9eecb35d6b3cd772b57620f37deb065b2f426aae-0c5d005cd383e2d8c2bea388419ac26ace12e0f0/bin/clang -cc1 -triple x86_64-unknown-linux-gnu -emit-llvm-bc -disable-free -main-file-name bar.c -mrelocation-model static -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-linker-version 2.20.1 -momit-leaf-frame-pointer -coverage-file /scratch/dgilmore/delta_bug669/bar.o -resource-dir /mips/proj/build-compiler/log.2012-11-19/LLVM-clang/install-master-9eecb35d6b3cd772b57620f37deb065b2f426aae-0c5d005cd383e2d8c2bea388419ac26ace12e0f0/bin/../lib/clang/3.3 -fmodule-cache-path /var/tmp/clang-module-cache -internal-isystem /usr/local/include -internal-isystem /mips/proj/build-compiler/log.2012-11-19/LLVM-clang/install-master-9eecb35d6b3cd772b57620f37deb065b2f426aae-0c5d005cd383e2d8c2bea388419ac26ace12e0f0/bin/../lib/clang/3.3/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir /scratch/dgilmore/delta_bug669 -ferror-limit 19 -fmessage-length 154 -mstackrealign -fobjc-runtime=gcc -fdiagnostics-show-option -o bar.o -x c bar.c
  2. bar.c:4:20 : current parser token ')'
  3. bar.c:2:1: parsing function body 'foo_aa'
  4. bar.c:2:1: in compound statement ('{}') clang: error: unable to execute command: Aborted clang: error: clang frontend command failed due to signal (use -v to see invocation) clang version 3.3 (git://dmz-portal.mips.com/clang.git 0c5d005cd383e2d8c2bea388419ac26ace12e0f0) (git://dmz-portal.mips.com/llvm.git 9eecb35d6b3cd772b57620f37deb065b2f426aae) Target: x86_64-unknown-linux-gnu Thread model: posix clang: note: diagnostic msg: PLEASE submit a bug report to http://llvm.org/bugs/ and include the crash backtrace, preprocessed source, and associated run script. clang: note: diagnostic msg:

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT: Preprocessed source(s) and associated run script(s) are located at: clang: note: diagnostic msg: /tmp/bar-3QBo5S.c clang: note: diagnostic msg: /tmp/bar-3QBo5S.sh clang: note: diagnostic msg:


$

efriedma-quic commented 11 years ago

r168984.

ec04fc15-fa35-46f2-80e1-5d271f2ef708 commented 11 years ago

That seems fine to me. I assume that means that the LHS range has one end pointing into the macro arg expansion and the other pointing into the macro expansion?

Yes, thinko, your highlighting looks right to me (what I proposed is closer to how we'd highlight if the caret pointed within the range).

ec04fc15-fa35-46f2-80e1-5d271f2ef708 commented 11 years ago

That seems fine to me. I assume that means that the LHS range has one end pointing into the macro arg expansion and the other pointing into the macro expansion?

efriedma-quic commented 11 years ago

Another example:

I tried implementing your approach, and I'm getting the following:

:3:9: warning: expression result unused [-Wunused-value] int k = FOO(ID(1) + 1, 2, 3); ^~~~~~~~~~~~~~~~~~~~ :1:24: note: expanded from macro 'FOO' #define FOO(a, ...) (a + __VA_ARGS__) ~ ^ I'm pretty sure this is the expected result, given how macro arguments work, but I figured I would double-check. (The first example works how you predicted.)
ec04fc15-fa35-46f2-80e1-5d271f2ef708 commented 11 years ago

How about we start by 'normalizing' each range into something we can highlight: find the lowest common ancestor FileID (by taking immediate expansion ranges only) of the beginning and end of each range, and use that. Then, since we must still have begin <= end, and we now have the same FileID for begin and end, we must have a contiguous range at every level in which we can highlight the range.

For the original example and for the example in comment#1, for the LHS range, we would step out of the macro argument expansion and into the macro expansion for BARC, highlighting the "a+b+c" expression. For the RHS range, we would keep the current range (contained entirely within the macro arg expansion for __VA_ARGS__).

That'd give something like this for the example in comment#0:

bar.c:4:20: warning: expression result unused [-Wunused-value] iequals(LINE, BARC(4,3,2,6,8), 8); ^~~~~~~

:3:50: note: expanded from macro 'BARC' #define /* */ BARC(c, /* */b, a, ...) (a+b+/* */c + __VA_ARGS__ +0) ~~~~~~~~~~ ^ (The __VA_ARGS__ is not highlighted because we jump over the macro expansion when getting the spelling location of the macro arg expansion. That's not ideal, but it's no worse than what we have now.) Another example: #define FOO(a, ...) (a + __VA_ARGS__) #define ID(x) x int k = FOO(ID(1) + 1, 2, 3); ... we would produce something like: foo.c:3:9: warning: expression result unused [-Wunused-value] int k = FOO(ID(1) + 1, 2, 3); ^ ~~~~~~~~~ ~ foo.c:1:24: note: expanded from macro 'FOO' #define FOO(a, ...) (a + __VA_ARGS__) ^ ... which looks much better than our current attempt of: int k = FOO(ID(1) + 1, 2, 3) ^ ~~~~~~ ~ :1:24: note: expanded from macro 'FOO' #define FOO(a, ...) (a + __VA_ARGS__) ^
efriedma-quic commented 11 years ago

Richard, do you have any ideas here? I have no clue how to solve this short of ripping out the whole getImmediateMacroCallerLoc/getImmediateMacroCalleeLoc concept; fundamentally, if we step into of a macro argument with getImmediateSpellingLoc(), we immediately lose track of the original macro argument expansion.

You can see more clearly what's happening with the following valid testcase:

void iequals(int,int,int); void foo_aa() {

define BARC(a, b, c, ...) (a+b+c + __VA_ARGS__)

iequals(__LINE__, BARC(4,3,2,6,8), 8);

}

:5:23: warning: expression result unused [-Wunused-value] iequals(__LINE__, BARC(4,3,2,6,8), 8); ^ ~~~~~ ~ :4:35: note: expanded from macro 'BARC' #define BARC(a, b, c, ...) (a+b+c + __VA_ARGS__) ^ Note the highlight across 4,3,2.