Skip to content

Commit 1e879d8

Browse files
committedMar 23, 2018
Sink PrettyDeclStackTrace down to the AST library
...and add some very basic stack trace entries for module building. This would have helped track down rdar://problem/38434694 sooner. llvm-svn: 328276
1 parent 37eeb32 commit 1e879d8

11 files changed

+55
-45
lines changed
 

‎clang/include/clang/Sema/PrettyDeclStackTrace.h ‎clang/include/clang/AST/PrettyDeclStackTrace.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,31 @@
1313
//
1414
//===----------------------------------------------------------------------===//
1515

16-
#ifndef LLVM_CLANG_SEMA_PRETTYDECLSTACKTRACE_H
17-
#define LLVM_CLANG_SEMA_PRETTYDECLSTACKTRACE_H
16+
#ifndef LLVM_CLANG_AST_PRETTYDECLSTACKTRACE_H
17+
#define LLVM_CLANG_AST_PRETTYDECLSTACKTRACE_H
1818

1919
#include "clang/Basic/SourceLocation.h"
2020
#include "llvm/Support/PrettyStackTrace.h"
2121

2222
namespace clang {
2323

24+
class ASTContext;
2425
class Decl;
25-
class Sema;
2626
class SourceManager;
2727

2828
/// PrettyDeclStackTraceEntry - If a crash occurs in the parser while
2929
/// parsing something related to a declaration, include that
3030
/// declaration in the stack trace.
3131
class PrettyDeclStackTraceEntry : public llvm::PrettyStackTraceEntry {
32-
Sema &S;
32+
ASTContext &Context;
3333
Decl *TheDecl;
3434
SourceLocation Loc;
3535
const char *Message;
3636

3737
public:
38-
PrettyDeclStackTraceEntry(Sema &S, Decl *D, SourceLocation Loc,
38+
PrettyDeclStackTraceEntry(ASTContext &Ctx, Decl *D, SourceLocation Loc,
3939
const char *Msg)
40-
: S(S), TheDecl(D), Loc(Loc), Message(Msg) {}
40+
: Context(Ctx), TheDecl(D), Loc(Loc), Message(Msg) {}
4141

4242
void print(raw_ostream &OS) const override;
4343
};

‎clang/lib/AST/Decl.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "clang/AST/ExprCXX.h"
2828
#include "clang/AST/ExternalASTSource.h"
2929
#include "clang/AST/ODRHash.h"
30+
#include "clang/AST/PrettyDeclStackTrace.h"
3031
#include "clang/AST/PrettyPrinter.h"
3132
#include "clang/AST/Redeclarable.h"
3233
#include "clang/AST/Stmt.h"
@@ -76,6 +77,24 @@ Decl *clang::getPrimaryMergedDecl(Decl *D) {
7677
return D->getASTContext().getPrimaryMergedDecl(D);
7778
}
7879

80+
void PrettyDeclStackTraceEntry::print(raw_ostream &OS) const {
81+
SourceLocation Loc = this->Loc;
82+
if (!Loc.isValid() && TheDecl) Loc = TheDecl->getLocation();
83+
if (Loc.isValid()) {
84+
Loc.print(OS, Context.getSourceManager());
85+
OS << ": ";
86+
}
87+
OS << Message;
88+
89+
if (auto *ND = dyn_cast_or_null<NamedDecl>(TheDecl)) {
90+
OS << " '";
91+
ND->getNameForDiagnostic(OS, Context.getPrintingPolicy(), true);
92+
OS << "'";
93+
}
94+
95+
OS << '\n';
96+
}
97+
7998
// Defined here so that it can be inlined into its direct callers.
8099
bool Decl::isOutOfLine() const {
81100
return !getLexicalDeclContext()->Equals(getDeclContext());

‎clang/lib/Frontend/CompilerInstance.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,11 @@ compileModuleImpl(CompilerInstance &ImportingInstance, SourceLocation ImportLoc,
11701170
llvm::CrashRecoveryContext CRC;
11711171
CRC.RunSafelyOnThread(
11721172
[&]() {
1173+
SmallString<64> CrashInfoMessage("While building module for '");
1174+
CrashInfoMessage += ModuleName;
1175+
CrashInfoMessage += "'";
1176+
llvm::PrettyStackTraceString CrashInfo(CrashInfoMessage.c_str());
1177+
11731178
GenerateModuleFromModuleMapAction Action;
11741179
Instance.ExecuteAction(Action);
11751180
},

‎clang/lib/Parse/ParseDecl.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
#include "clang/Parse/RAIIObjectsForParser.h"
1616
#include "clang/AST/ASTContext.h"
1717
#include "clang/AST/DeclTemplate.h"
18+
#include "clang/AST/PrettyDeclStackTrace.h"
1819
#include "clang/Basic/AddressSpaces.h"
1920
#include "clang/Basic/Attributes.h"
2021
#include "clang/Basic/CharInfo.h"
2122
#include "clang/Basic/TargetInfo.h"
2223
#include "clang/Parse/ParseDiagnostic.h"
2324
#include "clang/Sema/Lookup.h"
2425
#include "clang/Sema/ParsedTemplate.h"
25-
#include "clang/Sema/PrettyDeclStackTrace.h"
2626
#include "clang/Sema/Scope.h"
2727
#include "clang/Sema/SemaDiagnostic.h"
2828
#include "llvm/ADT/Optional.h"
@@ -3908,7 +3908,7 @@ void Parser::ParseStructDeclaration(
39083908
///
39093909
void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
39103910
unsigned TagType, Decl *TagDecl) {
3911-
PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3911+
PrettyDeclStackTraceEntry CrashInfo(Actions.Context, TagDecl, RecordLoc,
39123912
"parsing struct/union body");
39133913
assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
39143914

‎clang/lib/Parse/ParseDeclCXX.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "clang/Parse/Parser.h"
1515
#include "clang/AST/ASTContext.h"
1616
#include "clang/AST/DeclTemplate.h"
17+
#include "clang/AST/PrettyDeclStackTrace.h"
1718
#include "clang/Basic/Attributes.h"
1819
#include "clang/Basic/CharInfo.h"
1920
#include "clang/Basic/OperatorKinds.h"
@@ -22,7 +23,6 @@
2223
#include "clang/Parse/RAIIObjectsForParser.h"
2324
#include "clang/Sema/DeclSpec.h"
2425
#include "clang/Sema/ParsedTemplate.h"
25-
#include "clang/Sema/PrettyDeclStackTrace.h"
2626
#include "clang/Sema/Scope.h"
2727
#include "clang/Sema/SemaDiagnostic.h"
2828
#include "llvm/ADT/SmallString.h"
@@ -188,8 +188,8 @@ Parser::DeclGroupPtrTy Parser::ParseNamespace(DeclaratorContext Context,
188188
IdentLoc, Ident, T.getOpenLocation(),
189189
attrs.getList(), ImplicitUsingDirectiveDecl);
190190

191-
PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
192-
"parsing namespace");
191+
PrettyDeclStackTraceEntry CrashInfo(Actions.Context, NamespcDecl,
192+
NamespaceLoc, "parsing namespace");
193193

194194
// Parse the contents of the namespace. This includes parsing recovery on
195195
// any improperly nested namespaces.
@@ -3110,7 +3110,7 @@ void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
31103110
TagType == DeclSpec::TST_union ||
31113111
TagType == DeclSpec::TST_class) && "Invalid TagType!");
31123112

3113-
PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3113+
PrettyDeclStackTraceEntry CrashInfo(Actions.Context, TagDecl, RecordLoc,
31143114
"parsing struct/union/class body");
31153115

31163116
// Determine whether this is a non-nested class. Note that local

‎clang/lib/Parse/ParseObjc.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313

1414
#include "clang/Parse/Parser.h"
1515
#include "clang/AST/ASTContext.h"
16+
#include "clang/AST/PrettyDeclStackTrace.h"
1617
#include "clang/Basic/CharInfo.h"
1718
#include "clang/Parse/ParseDiagnostic.h"
1819
#include "clang/Parse/RAIIObjectsForParser.h"
1920
#include "clang/Sema/DeclSpec.h"
20-
#include "clang/Sema/PrettyDeclStackTrace.h"
2121
#include "clang/Sema/Scope.h"
2222
#include "llvm/ADT/SmallVector.h"
2323
#include "llvm/ADT/StringExtras.h"
@@ -2680,7 +2680,7 @@ void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
26802680
Decl *Parser::ParseObjCMethodDefinition() {
26812681
Decl *MDecl = ParseObjCMethodPrototype();
26822682

2683-
PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
2683+
PrettyDeclStackTraceEntry CrashInfo(Actions.Context, MDecl, Tok.getLocation(),
26842684
"parsing Objective-C method");
26852685

26862686
// parse optional ';'

‎clang/lib/Parse/ParseStmt.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
#include "clang/AST/PrettyDeclStackTrace.h"
1516
#include "clang/Basic/Attributes.h"
1617
#include "clang/Basic/PrettyStackTrace.h"
1718
#include "clang/Parse/Parser.h"
1819
#include "clang/Parse/RAIIObjectsForParser.h"
1920
#include "clang/Sema/DeclSpec.h"
2021
#include "clang/Sema/LoopHint.h"
21-
#include "clang/Sema/PrettyDeclStackTrace.h"
2222
#include "clang/Sema/Scope.h"
2323
#include "clang/Sema/TypoCorrection.h"
2424
using namespace clang;
@@ -1957,7 +1957,7 @@ Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
19571957
assert(Tok.is(tok::l_brace));
19581958
SourceLocation LBraceLoc = Tok.getLocation();
19591959

1960-
PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1960+
PrettyDeclStackTraceEntry CrashInfo(Actions.Context, Decl, LBraceLoc,
19611961
"parsing function body");
19621962

19631963
// Save and reset current vtordisp stack if we have entered a C++ method body.
@@ -1990,7 +1990,7 @@ Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
19901990
assert(Tok.is(tok::kw_try) && "Expected 'try'");
19911991
SourceLocation TryLoc = ConsumeToken();
19921992

1993-
PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1993+
PrettyDeclStackTraceEntry CrashInfo(Actions.Context, Decl, TryLoc,
19941994
"parsing function try block");
19951995

19961996
// Constructor initializer list?

‎clang/lib/Sema/Sema.cpp

+1-19
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "clang/AST/DeclObjC.h"
2020
#include "clang/AST/Expr.h"
2121
#include "clang/AST/ExprCXX.h"
22+
#include "clang/AST/PrettyDeclStackTrace.h"
2223
#include "clang/AST/StmtCXX.h"
2324
#include "clang/Basic/DiagnosticOptions.h"
2425
#include "clang/Basic/PartialDiagnostic.h"
@@ -31,7 +32,6 @@
3132
#include "clang/Sema/Initialization.h"
3233
#include "clang/Sema/MultiplexExternalSemaSource.h"
3334
#include "clang/Sema/ObjCMethodList.h"
34-
#include "clang/Sema/PrettyDeclStackTrace.h"
3535
#include "clang/Sema/Scope.h"
3636
#include "clang/Sema/ScopeInfo.h"
3737
#include "clang/Sema/SemaConsumer.h"
@@ -1525,24 +1525,6 @@ void ExternalSemaSource::ReadUndefinedButUsed(
15251525
void ExternalSemaSource::ReadMismatchingDeleteExpressions(llvm::MapVector<
15261526
FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &) {}
15271527

1528-
void PrettyDeclStackTraceEntry::print(raw_ostream &OS) const {
1529-
SourceLocation Loc = this->Loc;
1530-
if (!Loc.isValid() && TheDecl) Loc = TheDecl->getLocation();
1531-
if (Loc.isValid()) {
1532-
Loc.print(OS, S.getSourceManager());
1533-
OS << ": ";
1534-
}
1535-
OS << Message;
1536-
1537-
if (auto *ND = dyn_cast_or_null<NamedDecl>(TheDecl)) {
1538-
OS << " '";
1539-
ND->getNameForDiagnostic(OS, ND->getASTContext().getPrintingPolicy(), true);
1540-
OS << "'";
1541-
}
1542-
1543-
OS << '\n';
1544-
}
1545-
15461528
/// \brief Figure out if an expression could be turned into a call.
15471529
///
15481530
/// Use this when trying to recover from an error where the programmer may have

‎clang/lib/Sema/SemaTemplateInstantiate.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
#include "clang/AST/ASTMutationListener.h"
1919
#include "clang/AST/DeclTemplate.h"
2020
#include "clang/AST/Expr.h"
21+
#include "clang/AST/PrettyDeclStackTrace.h"
2122
#include "clang/Basic/LangOptions.h"
2223
#include "clang/Sema/DeclSpec.h"
2324
#include "clang/Sema/Initialization.h"
2425
#include "clang/Sema/Lookup.h"
25-
#include "clang/Sema/PrettyDeclStackTrace.h"
2626
#include "clang/Sema/Template.h"
2727
#include "clang/Sema/TemplateDeduction.h"
2828
#include "clang/Sema/TemplateInstCallback.h"
@@ -2026,7 +2026,7 @@ Sema::InstantiateClass(SourceLocation PointOfInstantiation,
20262026
if (Inst.isInvalid())
20272027
return true;
20282028
assert(!Inst.isAlreadyInstantiating() && "should have been caught by caller");
2029-
PrettyDeclStackTraceEntry CrashInfo(*this, Instantiation, SourceLocation(),
2029+
PrettyDeclStackTraceEntry CrashInfo(Context, Instantiation, SourceLocation(),
20302030
"instantiating class definition");
20312031

20322032
// Enter the scope of this instantiation. We don't use
@@ -2253,7 +2253,7 @@ bool Sema::InstantiateEnum(SourceLocation PointOfInstantiation,
22532253
return true;
22542254
if (Inst.isAlreadyInstantiating())
22552255
return false;
2256-
PrettyDeclStackTraceEntry CrashInfo(*this, Instantiation, SourceLocation(),
2256+
PrettyDeclStackTraceEntry CrashInfo(Context, Instantiation, SourceLocation(),
22572257
"instantiating enum definition");
22582258

22592259
// The instantiation is visible here, even if it was first declared in an
@@ -2329,7 +2329,7 @@ bool Sema::InstantiateInClassInitializer(
23292329
<< Instantiation;
23302330
return true;
23312331
}
2332-
PrettyDeclStackTraceEntry CrashInfo(*this, Instantiation, SourceLocation(),
2332+
PrettyDeclStackTraceEntry CrashInfo(Context, Instantiation, SourceLocation(),
23332333
"instantiating default member init");
23342334

23352335
// Enter the scope of this instantiation. We don't use PushDeclContext because

‎clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
#include "clang/AST/DependentDiagnostic.h"
1919
#include "clang/AST/Expr.h"
2020
#include "clang/AST/ExprCXX.h"
21+
#include "clang/AST/PrettyDeclStackTrace.h"
2122
#include "clang/AST/TypeLoc.h"
2223
#include "clang/Sema/Initialization.h"
2324
#include "clang/Sema/Lookup.h"
24-
#include "clang/Sema/PrettyDeclStackTrace.h"
2525
#include "clang/Sema/Template.h"
2626
#include "clang/Sema/TemplateInstCallback.h"
2727

@@ -3895,7 +3895,7 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
38953895
InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
38963896
if (Inst.isInvalid() || Inst.isAlreadyInstantiating())
38973897
return;
3898-
PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
3898+
PrettyDeclStackTraceEntry CrashInfo(Context, Function, SourceLocation(),
38993899
"instantiating function definition");
39003900

39013901
// The instantiation is visible here, even if it was first declared in an
@@ -4306,7 +4306,7 @@ void Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation,
43064306
InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
43074307
if (Inst.isInvalid() || Inst.isAlreadyInstantiating())
43084308
return;
4309-
PrettyDeclStackTraceEntry CrashInfo(*this, Var, SourceLocation(),
4309+
PrettyDeclStackTraceEntry CrashInfo(Context, Var, SourceLocation(),
43104310
"instantiating variable initializer");
43114311

43124312
// The instantiation is visible here, even if it was first declared in an
@@ -4419,7 +4419,7 @@ void Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation,
44194419
InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
44204420
if (Inst.isInvalid() || Inst.isAlreadyInstantiating())
44214421
return;
4422-
PrettyDeclStackTraceEntry CrashInfo(*this, Var, SourceLocation(),
4422+
PrettyDeclStackTraceEntry CrashInfo(Context, Var, SourceLocation(),
44234423
"instantiating variable definition");
44244424

44254425
// If we're performing recursive template instantiation, create our own
@@ -5223,7 +5223,7 @@ void Sema::PerformPendingInstantiations(bool LocalOnly) {
52235223
break;
52245224
}
52255225

5226-
PrettyDeclStackTraceEntry CrashInfo(*this, Var, SourceLocation(),
5226+
PrettyDeclStackTraceEntry CrashInfo(Context, Var, SourceLocation(),
52275227
"instantiating variable definition");
52285228
bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
52295229
TSK_ExplicitInstantiationDefinition;

‎clang/lib/Serialization/ASTWriterDecl.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "clang/AST/DeclTemplate.h"
1818
#include "clang/AST/DeclVisitor.h"
1919
#include "clang/AST/Expr.h"
20+
#include "clang/AST/PrettyDeclStackTrace.h"
2021
#include "clang/Basic/SourceManager.h"
2122
#include "clang/Serialization/ASTReader.h"
2223
#include "clang/Serialization/ASTWriter.h"
@@ -2227,6 +2228,9 @@ static bool isRequiredDecl(const Decl *D, ASTContext &Context,
22272228
}
22282229

22292230
void ASTWriter::WriteDecl(ASTContext &Context, Decl *D) {
2231+
PrettyDeclStackTraceEntry CrashInfo(Context, D, SourceLocation(),
2232+
"serializing");
2233+
22302234
// Determine the ID for this declaration.
22312235
serialization::DeclID ID;
22322236
assert(!D->isFromASTFile() && "should not be emitting imported decl");

0 commit comments

Comments
 (0)
Please sign in to comment.