Coverage Report

Created: 2018-07-22 10:17

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/Sema/SemaInternal.h
Line
Count
Source (jump to first uncovered line)
1
//===--- SemaInternal.h - Internal Sema Interfaces --------------*- C++ -*-===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// This file provides common API and #includes for the internal
11
// implementation of Sema.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#ifndef LLVM_CLANG_SEMA_SEMAINTERNAL_H
16
#define LLVM_CLANG_SEMA_SEMAINTERNAL_H
17
18
#include "clang/AST/ASTContext.h"
19
#include "clang/Sema/Lookup.h"
20
#include "clang/Sema/Sema.h"
21
#include "clang/Sema/SemaDiagnostic.h"
22
23
namespace clang {
24
25
1.27M
inline PartialDiagnostic Sema::PDiag(unsigned DiagID) {
26
1.27M
  return PartialDiagnostic(DiagID, Context.getDiagAllocator());
27
1.27M
}
28
29
inline bool
30
4.41M
FTIHasSingleVoidParameter(const DeclaratorChunk::FunctionTypeInfo &FTI) {
31
4.41M
  return FTI.NumParams == 1 && 
!FTI.isVariadic1.91M
&&
32
4.41M
         
FTI.Params[0].Ident == nullptr1.90M
&&
FTI.Params[0].Param799k
&&
33
4.41M
         
cast<ParmVarDecl>(FTI.Params[0].Param)->getType()->isVoidType()799k
;
34
4.41M
}
35
36
inline bool
37
5.34M
FTIHasNonVoidParameters(const DeclaratorChunk::FunctionTypeInfo &FTI) {
38
5.34M
  // Assume FTI is well-formed.
39
5.34M
  return FTI.NumParams && 
!FTIHasSingleVoidParameter(FTI)4.41M
;
40
5.34M
}
41
42
// This requires the variable to be non-dependent and the initializer
43
// to not be value dependent.
44
7.64M
inline bool IsVariableAConstantExpression(VarDecl *Var, ASTContext &Context) {
45
7.64M
  const VarDecl *DefVD = nullptr;
46
7.64M
  return !isa<ParmVarDecl>(Var) &&
47
7.64M
    
Var->isUsableInConstantExpressions(Context)3.77M
&&
48
7.64M
    
Var->getAnyInitializer(DefVD)866k
&&
DefVD->checkInitIsICE()864k
;
49
7.64M
}
50
51
// Helper function to check whether D's attributes match current CUDA mode.
52
// Decls with mismatched attributes and related diagnostics may have to be
53
// ignored during this CUDA compilation pass.
54
25.5k
inline bool DeclAttrsMatchCUDAMode(const LangOptions &LangOpts, Decl *D) {
55
25.5k
  if (!LangOpts.CUDA || 
!D28
)
56
25.5k
    return true;
57
28
  bool isDeviceSideDecl = D->hasAttr<CUDADeviceAttr>() ||
58
28
                          
D->hasAttr<CUDASharedAttr>()12
||
59
28
                          
D->hasAttr<CUDAGlobalAttr>()10
;
60
28
  return isDeviceSideDecl == LangOpts.CUDAIsDevice;
61
28
}
62
63
// Directly mark a variable odr-used. Given a choice, prefer to use 
64
// MarkVariableReferenced since it does additional checks and then 
65
// calls MarkVarDeclODRUsed.
66
// If the variable must be captured:
67
//  - if FunctionScopeIndexToStopAt is null, capture it in the CurContext
68
//  - else capture it in the DeclContext that maps to the 
69
//    *FunctionScopeIndexToStopAt on the FunctionScopeInfo stack.  
70
inline void MarkVarDeclODRUsed(VarDecl *Var,
71
    SourceLocation Loc, Sema &SemaRef,
72
6.90M
    const unsigned *const FunctionScopeIndexToStopAt) {
73
6.90M
  // Keep track of used but undefined variables.
74
6.90M
  // FIXME: We shouldn't suppress this warning for static data members.
75
6.90M
  if (Var->hasDefinition(SemaRef.Context) == VarDecl::DeclarationOnly &&
76
6.90M
      
(150k
!Var->isExternallyVisible()150k
||
Var->isInline()150k
||
77
150k
       
SemaRef.isExternalWithNoLinkageType(Var)150k
) &&
78
6.90M
      
!(109
Var->isStaticDataMember()109
&&
Var->hasInit()56
)) {
79
77
    SourceLocation &old = SemaRef.UndefinedButUsed[Var->getCanonicalDecl()];
80
77
    if (old.isInvalid())
81
74
      old = Loc;
82
77
  }
83
6.90M
  QualType CaptureType, DeclRefType;
84
6.90M
  SemaRef.tryCaptureVariable(Var, Loc, Sema::TryCapture_Implicit, 
85
6.90M
    /*EllipsisLoc*/ SourceLocation(),
86
6.90M
    /*BuildAndDiagnose*/ true, 
87
6.90M
    CaptureType, DeclRefType, 
88
6.90M
    FunctionScopeIndexToStopAt);
89
6.90M
90
6.90M
  Var->markUsed(SemaRef.Context);
91
6.90M
}
92
93
/// Return a DLL attribute from the declaration.
94
11.5M
inline InheritableAttr *getDLLAttr(Decl *D) {
95
11.5M
  assert(!(D->hasAttr<DLLImportAttr>() && D->hasAttr<DLLExportAttr>()) &&
96
11.5M
         "A declaration cannot be both dllimport and dllexport.");
97
11.5M
  if (auto *Import = D->getAttr<DLLImportAttr>())
98
5.42k
    return Import;
99
11.5M
  if (auto *Export = D->getAttr<DLLExportAttr>())
100
6.25k
    return Export;
101
11.5M
  return nullptr;
102
11.5M
}
103
104
class TypoCorrectionConsumer : public VisibleDeclConsumer {
105
  typedef SmallVector<TypoCorrection, 1> TypoResultList;
106
  typedef llvm::StringMap<TypoResultList> TypoResultsMap;
107
  typedef std::map<unsigned, TypoResultsMap> TypoEditDistanceMap;
108
109
public:
110
  TypoCorrectionConsumer(Sema &SemaRef,
111
                         const DeclarationNameInfo &TypoName,
112
                         Sema::LookupNameKind LookupKind,
113
                         Scope *S, CXXScopeSpec *SS,
114
                         std::unique_ptr<CorrectionCandidateCallback> CCC,
115
                         DeclContext *MemberContext,
116
                         bool EnteringContext)
117
      : Typo(TypoName.getName().getAsIdentifierInfo()), CurrentTCIndex(0),
118
        SavedTCIndex(0), SemaRef(SemaRef), S(S),
119
        SS(SS ? llvm::make_unique<CXXScopeSpec>(*SS) : nullptr),
120
        CorrectionValidator(std::move(CCC)), MemberContext(MemberContext),
121
        Result(SemaRef, TypoName, LookupKind),
122
        Namespaces(SemaRef.Context, SemaRef.CurContext, SS),
123
3.53k
        EnteringContext(EnteringContext), SearchNamespaces(false) {
124
3.53k
    Result.suppressDiagnostics();
125
3.53k
    // Arrange for ValidatedCorrections[0] to always be an empty correction.
126
3.53k
    ValidatedCorrections.push_back(TypoCorrection());
127
3.53k
  }
128
129
587
  bool includeHiddenDecls() const override { return true; }
130
131
  // Methods for adding potential corrections to the consumer.
132
  void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx,
133
                 bool InBaseClass) override;
134
  void FoundName(StringRef Name);
135
  void addKeywordResult(StringRef Keyword);
136
  void addCorrection(TypoCorrection Correction);
137
138
3.37k
  bool empty() const {
139
3.37k
    return CorrectionResults.empty() && 
ValidatedCorrections.size() == 1112
;
140
3.37k
  }
141
142
  /// Return the list of TypoCorrections for the given identifier from
143
  /// the set of corrections that have the closest edit distance, if any.
144
0
  TypoResultList &operator[](StringRef Name) {
145
0
    return CorrectionResults.begin()->second[Name];
146
0
  }
147
148
  /// Return the edit distance of the corrections that have the
149
  /// closest/best edit distance from the original typop.
150
3.26k
  unsigned getBestEditDistance(bool Normalized) {
151
3.26k
    if (CorrectionResults.empty())
152
0
      return (std::numeric_limits<unsigned>::max)();
153
3.26k
154
3.26k
    unsigned BestED = CorrectionResults.begin()->first;
155
3.26k
    return Normalized ? TypoCorrection::NormalizeEditDistance(BestED) : 
BestED0
;
156
3.26k
  }
157
158
  /// Set-up method to add to the consumer the set of namespaces to use
159
  /// in performing corrections to nested name specifiers. This method also
160
  /// implicitly adds all of the known classes in the current AST context to the
161
  /// to the consumer for correcting nested name specifiers.
162
  void
163
  addNamespaces(const llvm::MapVector<NamespaceDecl *, bool> &KnownNamespaces);
164
165
  /// Return the next typo correction that passes all internal filters
166
  /// and is deemed valid by the consumer's CorrectionCandidateCallback,
167
  /// starting with the corrections that have the closest edit distance. An
168
  /// empty TypoCorrection is returned once no more viable corrections remain
169
  /// in the consumer.
170
  const TypoCorrection &getNextCorrection();
171
172
  /// Get the last correction returned by getNextCorrection().
173
1.07k
  const TypoCorrection &getCurrentCorrection() {
174
1.07k
    return CurrentTCIndex < ValidatedCorrections.size()
175
1.07k
               ? ValidatedCorrections[CurrentTCIndex]
176
1.07k
               : 
ValidatedCorrections[0]0
; // The empty correction.
177
1.07k
  }
178
179
  /// Return the next typo correction like getNextCorrection, but keep
180
  /// the internal state pointed to the current correction (i.e. the next time
181
  /// getNextCorrection is called, it will return the same correction returned
182
  /// by peekNextcorrection).
183
538
  const TypoCorrection &peekNextCorrection() {
184
538
    auto Current = CurrentTCIndex;
185
538
    const TypoCorrection &TC = getNextCorrection();
186
538
    CurrentTCIndex = Current;
187
538
    return TC;
188
538
  }
189
190
  /// Reset the consumer's position in the stream of viable corrections
191
  /// (i.e. getNextCorrection() will return each of the previously returned
192
  /// corrections in order before returning any new corrections).
193
1.42k
  void resetCorrectionStream() {
194
1.42k
    CurrentTCIndex = 0;
195
1.42k
  }
196
197
  /// Return whether the end of the stream of corrections has been
198
  /// reached.
199
913
  bool finished() {
200
913
    return CorrectionResults.empty() &&
201
913
           
CurrentTCIndex >= ValidatedCorrections.size()803
;
202
913
  }
203
204
  /// Save the current position in the correction stream (overwriting any
205
  /// previously saved position).
206
44
  void saveCurrentPosition() {
207
44
    SavedTCIndex = CurrentTCIndex;
208
44
  }
209
210
  /// Restore the saved position in the correction stream.
211
4
  void restoreSavedPosition() {
212
4
    CurrentTCIndex = SavedTCIndex;
213
4
  }
214
215
0
  ASTContext &getContext() const { return SemaRef.Context; }
216
854
  const LookupResult &getLookupResult() const { return Result; }
217
218
63
  bool isAddressOfOperand() const { return CorrectionValidator->IsAddressOfOperand; }
219
427
  const CXXScopeSpec *getSS() const { return SS.get(); }
220
17
  Scope *getScope() const { return S; }
221
86
  CorrectionCandidateCallback *getCorrectionValidator() const {
222
86
    return CorrectionValidator.get();
223
86
  }
224
225
private:
226
  class NamespaceSpecifierSet {
227
    struct SpecifierInfo {
228
      DeclContext* DeclCtx;
229
      NestedNameSpecifier* NameSpecifier;
230
      unsigned EditDistance;
231
    };
232
233
    typedef SmallVector<DeclContext*, 4> DeclContextList;
234
    typedef SmallVector<SpecifierInfo, 16> SpecifierInfoList;
235
236
    ASTContext &Context;
237
    DeclContextList CurContextChain;
238
    std::string CurNameSpecifier;
239
    SmallVector<const IdentifierInfo*, 4> CurContextIdentifiers;
240
    SmallVector<const IdentifierInfo*, 4> CurNameSpecifierIdentifiers;
241
242
    std::map<unsigned, SpecifierInfoList> DistanceMap;
243
244
    /// Helper for building the list of DeclContexts between the current
245
    /// context and the top of the translation unit
246
    static DeclContextList buildContextChain(DeclContext *Start);
247
248
    unsigned buildNestedNameSpecifier(DeclContextList &DeclChain,
249
                                      NestedNameSpecifier *&NNS);
250
251
   public:
252
    NamespaceSpecifierSet(ASTContext &Context, DeclContext *CurContext,
253
                          CXXScopeSpec *CurScopeSpec);
254
255
    /// Add the DeclContext (a namespace or record) to the set, computing
256
    /// the corresponding NestedNameSpecifier and its distance in the process.
257
    void addNameSpecifier(DeclContext *Ctx);
258
259
    /// Provides flat iteration over specifiers, sorted by distance.
260
    class iterator
261
        : public llvm::iterator_facade_base<iterator, std::forward_iterator_tag,
262
                                            SpecifierInfo> {
263
      /// Always points to the last element in the distance map.
264
      const std::map<unsigned, SpecifierInfoList>::iterator OuterBack;
265
      /// Iterator on the distance map.
266
      std::map<unsigned, SpecifierInfoList>::iterator Outer;
267
      /// Iterator on an element in the distance map.
268
      SpecifierInfoList::iterator Inner;
269
270
    public:
271
      iterator(NamespaceSpecifierSet &Set, bool IsAtEnd)
272
          : OuterBack(std::prev(Set.DistanceMap.end())),
273
            Outer(Set.DistanceMap.begin()),
274
5.85k
            Inner(!IsAtEnd ? Outer->second.begin() : OuterBack->second.end()) {
275
5.85k
        assert(!Set.DistanceMap.empty());
276
5.85k
      }
277
278
84.9k
      iterator &operator++() {
279
84.9k
        ++Inner;
280
84.9k
        if (Inner == Outer->second.end() && 
Outer != OuterBack5.43k
) {
281
2.51k
          ++Outer;
282
2.51k
          Inner = Outer->second.begin();
283
2.51k
        }
284
84.9k
        return *this;
285
84.9k
      }
286
287
84.9k
      SpecifierInfo &operator*() { return *Inner; }
288
87.8k
      bool operator==(const iterator &RHS) const { return Inner == RHS.Inner; }
289
    };
290
291
2.92k
    iterator begin() { return iterator(*this, /*IsAtEnd=*/false); }
292
2.92k
    iterator end() { return iterator(*this, /*IsAtEnd=*/true); }
293
  };
294
295
  void addName(StringRef Name, NamedDecl *ND,
296
               NestedNameSpecifier *NNS = nullptr, bool isKeyword = false);
297
298
  /// Find any visible decls for the given typo correction candidate.
299
  /// If none are found, it to the set of candidates for which qualified lookups
300
  /// will be performed to find possible nested name specifier changes.
301
  bool resolveCorrection(TypoCorrection &Candidate);
302
303
  /// Perform qualified lookups on the queued set of typo correction
304
  /// candidates and add the nested name specifier changes to each candidate if
305
  /// a lookup succeeds (at which point the candidate will be returned to the
306
  /// main pool of potential corrections).
307
  void performQualifiedLookups();
308
309
  /// The name written that is a typo in the source.
310
  IdentifierInfo *Typo;
311
312
  /// The results found that have the smallest edit distance
313
  /// found (so far) with the typo name.
314
  ///
315
  /// The pointer value being set to the current DeclContext indicates
316
  /// whether there is a keyword with this name.
317
  TypoEditDistanceMap CorrectionResults;
318
319
  SmallVector<TypoCorrection, 4> ValidatedCorrections;
320
  size_t CurrentTCIndex;
321
  size_t SavedTCIndex;
322
323
  Sema &SemaRef;
324
  Scope *S;
325
  std::unique_ptr<CXXScopeSpec> SS;
326
  std::unique_ptr<CorrectionCandidateCallback> CorrectionValidator;
327
  DeclContext *MemberContext;
328
  LookupResult Result;
329
  NamespaceSpecifierSet Namespaces;
330
  SmallVector<TypoCorrection, 2> QualifiedResults;
331
  bool EnteringContext;
332
  bool SearchNamespaces;
333
};
334
335
1.08k
inline Sema::TypoExprState::TypoExprState() {}
336
337
2.18k
inline Sema::TypoExprState::TypoExprState(TypoExprState &&other) noexcept {
338
2.18k
  *this = std::move(other);
339
2.18k
}
340
341
inline Sema::TypoExprState &Sema::TypoExprState::
342
2.23k
operator=(Sema::TypoExprState &&other) noexcept {
343
2.23k
  Consumer = std::move(other.Consumer);
344
2.23k
  DiagHandler = std::move(other.DiagHandler);
345
2.23k
  RecoveryHandler = std::move(other.RecoveryHandler);
346
2.23k
  return *this;
347
2.23k
}
348
349
} // end namespace clang
350
351
#endif