Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Parse/ParseAST.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- ParseAST.cpp - Provide the clang::ParseAST method ----------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file implements the clang::ParseAST method.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/Parse/ParseAST.h"
14
#include "clang/AST/ASTConsumer.h"
15
#include "clang/AST/ASTContext.h"
16
#include "clang/AST/ExternalASTSource.h"
17
#include "clang/AST/Stmt.h"
18
#include "clang/Parse/ParseDiagnostic.h"
19
#include "clang/Parse/Parser.h"
20
#include "clang/Sema/CodeCompleteConsumer.h"
21
#include "clang/Sema/Sema.h"
22
#include "clang/Sema/SemaConsumer.h"
23
#include "clang/Sema/TemplateInstCallback.h"
24
#include "llvm/Support/CrashRecoveryContext.h"
25
#include "llvm/Support/TimeProfiler.h"
26
#include <cstdio>
27
#include <memory>
28
29
using namespace clang;
30
31
namespace {
32
33
/// Resets LLVM's pretty stack state so that stack traces are printed correctly
34
/// when there are nested CrashRecoveryContexts and the inner one recovers from
35
/// a crash.
36
class ResetStackCleanup
37
    : public llvm::CrashRecoveryContextCleanupBase<ResetStackCleanup,
38
                                                   const void> {
39
public:
40
  ResetStackCleanup(llvm::CrashRecoveryContext *Context, const void *Top)
41
      : llvm::CrashRecoveryContextCleanupBase<ResetStackCleanup, const void>(
42
0
            Context, Top) {}
43
0
  void recoverResources() override {
44
0
    llvm::RestorePrettyStackState(resource);
45
0
  }
46
};
47
48
/// If a crash happens while the parser is active, an entry is printed for it.
49
class PrettyStackTraceParserEntry : public llvm::PrettyStackTraceEntry {
50
  const Parser &P;
51
public:
52
41.4k
  PrettyStackTraceParserEntry(const Parser &p) : P(p) {}
53
  void print(raw_ostream &OS) const override;
54
};
55
56
/// If a crash happens while the parser is active, print out a line indicating
57
/// what the current token is.
58
0
void PrettyStackTraceParserEntry::print(raw_ostream &OS) const {
59
0
  const Token &Tok = P.getCurToken();
60
0
  if (Tok.is(tok::eof)) {
61
0
    OS << "<eof> parser at end of file\n";
62
0
    return;
63
0
  }
64
0
65
0
  if (Tok.getLocation().isInvalid()) {
66
0
    OS << "<unknown> parser at unknown location\n";
67
0
    return;
68
0
  }
69
0
70
0
  const Preprocessor &PP = P.getPreprocessor();
71
0
  Tok.getLocation().print(OS, PP.getSourceManager());
72
0
  if (Tok.isAnnotation()) {
73
0
    OS << ": at annotation token\n";
74
0
  } else {
75
0
    // Do the equivalent of PP.getSpelling(Tok) except for the parts that would
76
0
    // allocate memory.
77
0
    bool Invalid = false;
78
0
    const SourceManager &SM = P.getPreprocessor().getSourceManager();
79
0
    unsigned Length = Tok.getLength();
80
0
    const char *Spelling = SM.getCharacterData(Tok.getLocation(), &Invalid);
81
0
    if (Invalid) {
82
0
      OS << ": unknown current parser token\n";
83
0
      return;
84
0
    }
85
0
    OS << ": current parser token '" << StringRef(Spelling, Length) << "'\n";
86
0
  }
87
0
}
88
89
}  // namespace
90
91
//===----------------------------------------------------------------------===//
92
// Public interface to the file
93
//===----------------------------------------------------------------------===//
94
95
/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
96
/// the file is parsed.  This inserts the parsed decls into the translation unit
97
/// held by Ctx.
98
///
99
void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
100
                     ASTContext &Ctx, bool PrintStats,
101
                     TranslationUnitKind TUKind,
102
                     CodeCompleteConsumer *CompletionConsumer,
103
113
                     bool SkipFunctionBodies) {
104
113
105
113
  std::unique_ptr<Sema> S(
106
113
      new Sema(PP, Ctx, *Consumer, TUKind, CompletionConsumer));
107
113
108
113
  // Recover resources if we crash before exiting this method.
109
113
  llvm::CrashRecoveryContextCleanupRegistrar<Sema> CleanupSema(S.get());
110
113
111
113
  ParseAST(*S.get(), PrintStats, SkipFunctionBodies);
112
113
}
113
114
41.4k
void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) {
115
41.4k
  // Collect global stats on Decls/Stmts (until we have a module streamer).
116
41.4k
  if (PrintStats) {
117
3
    Decl::EnableStatistics();
118
3
    Stmt::EnableStatistics();
119
3
  }
120
41.4k
121
41.4k
  // Also turn on collection of stats inside of the Sema object.
122
41.4k
  bool OldCollectStats = PrintStats;
123
41.4k
  std::swap(OldCollectStats, S.CollectStats);
124
41.4k
125
41.4k
  // Initialize the template instantiation observer chain.
126
41.4k
  // FIXME: See note on "finalize" below.
127
41.4k
  initialize(S.TemplateInstCallbacks, S);
128
41.4k
129
41.4k
  ASTConsumer *Consumer = &S.getASTConsumer();
130
41.4k
131
41.4k
  std::unique_ptr<Parser> ParseOP(
132
41.4k
      new Parser(S.getPreprocessor(), S, SkipFunctionBodies));
133
41.4k
  Parser &P = *ParseOP.get();
134
41.4k
135
41.4k
  llvm::CrashRecoveryContextCleanupRegistrar<const void, ResetStackCleanup>
136
41.4k
      CleanupPrettyStack(llvm::SavePrettyStackState());
137
41.4k
  PrettyStackTraceParserEntry CrashInfo(P);
138
41.4k
139
41.4k
  // Recover resources if we crash before exiting this method.
140
41.4k
  llvm::CrashRecoveryContextCleanupRegistrar<Parser>
141
41.4k
    CleanupParser(ParseOP.get());
142
41.4k
143
41.4k
  S.getPreprocessor().EnterMainSourceFile();
144
41.4k
  ExternalASTSource *External = S.getASTContext().getExternalSource();
145
41.4k
  if (External)
146
5.18k
    External->StartTranslationUnit(Consumer);
147
41.4k
148
41.4k
  // If a PCH through header is specified that does not have an include in
149
41.4k
  // the source, or a PCH is being created with #pragma hdrstop with nothing
150
41.4k
  // after the pragma, there won't be any tokens or a Lexer.
151
41.4k
  bool HaveLexer = S.getPreprocessor().getCurrentLexer();
152
41.4k
153
41.4k
  if (HaveLexer) {
154
41.4k
    llvm::TimeTraceScope TimeScope("Frontend", StringRef(""));
155
41.4k
    P.Initialize();
156
41.4k
    Parser::DeclGroupPtrTy ADecl;
157
3.87M
    for (bool AtEOF = P.ParseFirstTopLevelDecl(ADecl); !AtEOF;
158
3.83M
         AtEOF = P.ParseTopLevelDecl(ADecl)) {
159
3.83M
      // If we got a null return and something *was* parsed, ignore it.  This
160
3.83M
      // is due to a top-level semicolon, an action override, or a parse error
161
3.83M
      // skipping something.
162
3.83M
      if (ADecl && 
!Consumer->HandleTopLevelDecl(ADecl.get())3.75M
)
163
0
        return;
164
3.83M
    }
165
41.4k
  }
166
41.4k
167
41.4k
  // Process any TopLevelDecls generated by #pragma weak.
168
41.4k
  for (Decl *D : S.WeakTopLevelDecls())
169
17
    Consumer->HandleTopLevelDecl(DeclGroupRef(D));
170
41.4k
171
41.4k
  Consumer->HandleTranslationUnit(S.getASTContext());
172
41.4k
173
41.4k
  // Finalize the template instantiation observer chain.
174
41.4k
  // FIXME: This (and init.) should be done in the Sema class, but because
175
41.4k
  // Sema does not have a reliable "Finalize" function (it has a
176
41.4k
  // destructor, but it is not guaranteed to be called ("-disable-free")).
177
41.4k
  // So, do the initialization above and do the finalization here:
178
41.4k
  finalize(S.TemplateInstCallbacks, S);
179
41.4k
180
41.4k
  std::swap(OldCollectStats, S.CollectStats);
181
41.4k
  if (PrintStats) {
182
3
    llvm::errs() << "\nSTATISTICS:\n";
183
3
    if (HaveLexer) P.getActions().PrintStats();
184
3
    S.getASTContext().PrintStats();
185
3
    Decl::PrintStats();
186
3
    Stmt::PrintStats();
187
3
    Consumer->PrintStats();
188
3
  }
189
41.4k
}