llvm / llvm-project

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

Clang crashes when instantiating a function template which uses `decltype` and `typedef` #19118

Open llvmbot opened 10 years ago

llvmbot commented 10 years ago
Bugzilla Link 18744
Version 3.4
OS Linux
Reporter LLVM Bugzilla Contributor
CC @DougGregor

Extended Description

Clang crashes when instantiating a function template which uses decltype as the return type and inside the function a decltype typedef. The error only appears when you compile with PCH (precompiled header).

Stackdump:

0  libLLVM-3.3.so  0x0000003920951672 llvm::sys::PrintStackTrace(_IO_FILE*) + 50
1  libLLVM-3.3.so  0x00000039209514a4
2  libpthread.so.0 0x00000039c860f750
3  clang           0x00000000010fd16c
4  clang           0x00000000010fd2c2
5  clang           0x000000000062a973
6  clang           0x00000000010feb60
7  clang           0x00000000010ffcb2 clang::Stmt::Profile(llvm::FoldingSetNodeID&, clang::ASTContext const&, bool) const + 2962
8  clang           0x0000000000ff1974 clang::ASTContext::getDecltypeType(clang::Expr*, clang::QualType) const + 100
9  clang           0x00000000007308a8 clang::ASTReader::readTypeRecord(unsigned int) + 4728
10 clang           0x00000000007313bd clang::ASTReader::GetType(unsigned int) + 141
11 clang           0x0000000000731983 clang::ASTReader::getLocalType(clang::serialization::ModuleFile&, unsigned int) + 19
12 clang           0x000000000074d337
13 clang           0x000000000072fc23 clang::ASTReader::readTypeRecord(unsigned int) + 1523
14 clang           0x00000000007313bd clang::ASTReader::GetType(unsigned int) + 141
15 clang           0x0000000000731983 clang::ASTReader::getLocalType(clang::serialization::ModuleFile&, unsigned int) + 19
16 clang           0x000000000075d53b clang::ASTDeclReader::VisitValueDecl(clang::ValueDecl*) + 75
17 clang           0x000000000075d67f clang::ASTDeclReader::VisitDeclaratorDecl(clang::DeclaratorDecl*) + 15
18 clang           0x000000000075dbc0 clang::ASTDeclReader::VisitFunctionDecl(clang::FunctionDecl*) + 192
19 clang           0x000000000076415d clang::ASTDeclReader::Visit(clang::Decl*) + 445
20 clang           0x0000000000764b44 clang::ASTReader::ReadDeclRecord(unsigned int) + 1044
21 clang           0x0000000000726207 clang::ASTReader::GetDecl(unsigned int) + 231
22 clang           0x000000000075b134 clang::ASTDeclReader::VisitDecl(clang::Decl*) + 100
23 clang           0x000000000075b7a1 clang::ASTDeclReader::VisitNamedDecl(clang::NamedDecl*) + 17
24 clang           0x000000000075d501 clang::ASTDeclReader::VisitValueDecl(clang::ValueDecl*) + 17
25 clang           0x000000000075d67f clang::ASTDeclReader::VisitDeclaratorDecl(clang::DeclaratorDecl*) + 15
26 clang           0x000000000075ef66 clang::ASTDeclReader::VisitVarDecl(clang::VarDecl*) + 182
27 clang           0x000000000075f37f clang::ASTDeclReader::VisitParmVarDecl(clang::ParmVarDecl*) + 15
28 clang           0x00000000007640bd clang::ASTDeclReader::Visit(clang::Decl*) + 285
29 clang           0x0000000000764b44 clang::ASTReader::ReadDeclRecord(unsigned int) + 1044
30 clang           0x0000000000726207 clang::ASTReader::GetDecl(unsigned int) + 231
31 clang           0x00000000007765a4 clang::ASTStmtReader::VisitDeclRefExpr(clang::DeclRefExpr*) + 356
32 clang           0x000000000077888b clang::ASTReader::ReadStmtFromStream(clang::serialization::ModuleFile&) + 2875
33 clang           0x0000000000730899 clang::ASTReader::readTypeRecord(unsigned int) + 4713
34 clang           0x00000000007313bd clang::ASTReader::GetType(unsigned int) + 141
35 clang           0x0000000000731983 clang::ASTReader::getLocalType(clang::serialization::ModuleFile&, unsigned int) + 19
36 clang           0x000000000074d337
37 clang           0x0000000000730c33 clang::ASTReader::readTypeRecord(unsigned int) + 5635
38 clang           0x00000000007313bd clang::ASTReader::GetType(unsigned int) + 141
39 clang           0x0000000000764040 clang::ASTDeclReader::Visit(clang::Decl*) + 160
40 clang           0x0000000000764b44 clang::ASTReader::ReadDeclRecord(unsigned int) + 1044
41 clang           0x0000000000726207 clang::ASTReader::GetDecl(unsigned int) + 231
42 clang           0x0000000000730c1f clang::ASTReader::readTypeRecord(unsigned int) + 5615
43 clang           0x00000000007313bd clang::ASTReader::GetType(unsigned int) + 141
44 clang           0x0000000000731983 clang::ASTReader::getLocalType(clang::serialization::ModuleFile&, unsigned int) + 19
45 clang           0x000000000074d337
46 clang           0x000000000072fd6e clang::ASTReader::readTypeRecord(unsigned int) + 1854
47 clang           0x00000000007313bd clang::ASTReader::GetType(unsigned int) + 141
48 clang           0x0000000000731983 clang::ASTReader::getLocalType(clang::serialization::ModuleFile&, unsigned int) + 19
49 clang           0x000000000074d337
50 clang           0x000000000073226d clang::ASTReader::GetTypeSourceInfo(clang::serialization::ModuleFile&, llvm::SmallVector<unsigned long, 64u> const&, unsigned int&) + 45
51 clang           0x0000000000752879 clang::ASTDeclReader::ReadCXXDefinitionData(clang::CXXRecordDecl::DefinitionData&, llvm::SmallVector<unsigned long, 64u> const&, unsigned int&) + 1641
52 clang           0x000000000075c49e clang::ASTDeclReader::VisitCXXRecordDecl(clang::CXXRecordDecl*) + 174
53 clang           0x000000000076422d clang::ASTDeclReader::Visit(clang::Decl*) + 653
54 clang           0x0000000000764b44 clang::ASTReader::ReadDeclRecord(unsigned int) + 1044
55 clang           0x0000000000726207 clang::ASTReader::GetDecl(unsigned int) + 231
56 clang           0x0000000000730fce clang::ASTReader::readTypeRecord(unsigned int) + 6558
57 clang           0x00000000007313bd clang::ASTReader::GetType(unsigned int) + 141
58 clang           0x0000000000731983 clang::ASTReader::getLocalType(clang::serialization::ModuleFile&, unsigned int) + 19
59 clang           0x000000000076d0fb clang::ASTStmtReader::VisitExpr(clang::Expr*) + 75
60 clang           0x00000000007746f9 clang::ASTStmtReader::VisitLambdaExpr(clang::LambdaExpr*) + 25
61 clang           0x000000000077888b clang::ASTReader::ReadStmtFromStream(clang::serialization::ModuleFile&) + 2875
62 clang           0x000000000072cefb clang::ASTReader::GetExternalDeclStmt(unsigned long) + 107
63 clang           0x00000000010508d6 clang::FunctionDecl::getBody(clang::FunctionDecl const*&) const + 118
64 clang           0x0000000000c76da8 clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool) + 184
65 clang           0x0000000000c76ada clang::Sema::PerformPendingInstantiations(bool) + 490
66 clang           0x00000000009bfa9a clang::Sema::ActOnEndOfTranslationUnit() + 3194
67 clang           0x000000000092fc76 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&) + 486
68 clang           0x0000000000927deb clang::ParseAST(clang::Sema&, bool, bool) + 571
69 clang           0x00000000007c8da2 clang::CodeGenAction::ExecuteAction() + 50
70 clang           0x000000000066bdf9 clang::FrontendAction::Execute() + 169
71 clang           0x000000000064bc65 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) + 309
72 clang           0x000000000063538c clang::ExecuteCompilerInvocation(clang::CompilerInstance*) + 1724
73 clang           0x000000000062ea10 cc1_main(char const**, char const**, char const*, void*) + 1072
74 clang           0x000000000062d041 main + 7841
75 libc.so.6       0x00000039c7e21d65 __libc_start_main + 245
76 clang           0x000000000062d915

Clang invocation:

/usr/bin/clang -cc1 -triple x86_64-redhat-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name CCampaignHandler.cpp -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -target-linker-version 2.23.2 -g -D ""M_BIN_DIR=\"/usr/local/bin\""" -D ""M_DATA_DIR=\"/usr/local/share/vcmi\""" -D ""M_LIB_DIR=\"/usr/local/lib64/vcmi\""" -D "vcmi_EXPORTS" -Wall -Wextra -Wpointer-arith -Wno-switch -Wno-sign-compare -Wno-unused-parameter -Wuninitialized -Wno-overloaded-virtual -Wno-mismatched-tags -std=c++0x -fdeprecated-macro -ferror-limit 19 -fmessage-length 0 -mstackrealign -fobjc-runtime=gcc -fobjc-default-synthesize-properties -fcxx-exceptions -fexceptions -fdiagnostics-show-option -backend-option -vectorize-loops -x c++ CCampaignHandler-DqmNlF.cpp

Template function:

    //Returns iterator to the element for which the value of ValueFunction is maximal
    template<class ForwardRange, class ValueFunction>
    auto maxElementByFun(const ForwardRange& rng, ValueFunction vf) -> decltype(std::begin(rng))
    {
        typedef decltype(*std::begin(rng)) ElemType;
        return boost::max_element(rng, [&] (ElemType lhs, ElemType rhs) -> bool
        {
            return vf(lhs) < vf(rhs);
        });
    }

If no ElemType typedef is used, Clang doesn't crash.

llvmbot commented 10 years ago

Sorry but I don't have that much time on my hands. Either you try and figure out how to reproduce the issue and provide details (what file from VCMI sources and what flags) or attach preprocessed file. The other option is to wait for someone else to pick this up, but that might take a while.

llvmbot commented 10 years ago

I don't know perhaps you can debug into Clang and find the source of the problem quicker. Therefore you should build VCMI. If you want I can provide you with information.

llvmbot commented 10 years ago

I tested the function template in a separate small project where PCH enabled/disabled didn't matter. When using the function template in the real scenario (this project: https://github.com/vcmi/vcmi) and having PCH enabled it crashes. In both scenarios I used CMake and Cotire (plugin for generating PCHs).

I compiled the preprocessed.cpp file as you said with the original compiler arguments and the additions you provided. This will generate something like a preprocessed header file. I feed the file to Clang and received no compiler errors.

llvmbot commented 10 years ago

I'm a bit confused now, first you said that this only happens when using precompiled header but later you say that pch doesn't matter?

I'm guessing that with this file being a part of larger project you're using some kind of build system. Try extracting the exact command line that invokes clang and just append -E > preprocessed.cpp. Feed the new file to clang and see if it crashes. Upload it if it does.

If it doesn't we'll have to try precompiled headers :(

llvmbot commented 10 years ago

Link to download: https://www.dropbox.com/s/f3fydsgpz8e3rtr/vcmi_CXX_prefix.hxx.gch

Preprocessed file was created with CMake's plugin Cotire.

llvmbot commented 10 years ago

Yeah, give it a try and upload the whole file somewhere if you fail to reduce it.

llvmbot commented 10 years ago

The file is very large around 56 MB. Perhaps I find a minimal example where this bug occurs.

llvmbot commented 10 years ago

Can you provide a preprocessed file?

llvmbot commented 10 years ago

When I use that function template in a separate C++ project which consists only of that template, main function and the template instantiation I don't get any compile error. It works with boost::max_element and std::max_element, PCH enabled/disabled doesn't matter. So, I don't know why this bug occurs.

llvmbot commented 10 years ago

Could you provide a minimal example that crashes? STL and Boost dependencies make this hard enough, precompiled headers more so :)

Maybe try ripping the relevant declarations from the standard library and boost and put them in a dummy header to generate small pch...