Coverage Report

Created: 2019-07-24 05:18

/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
// 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 provides common API and #includes for the internal
10
// implementation of Sema.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_CLANG_SEMA_SEMAINTERNAL_H
15
#define LLVM_CLANG_SEMA_SEMAINTERNAL_H
16
17
#include "clang/AST/ASTContext.h"
18
#include "clang/Sema/Lookup.h"
19
#include "clang/Sema/Sema.h"
20
#include "clang/Sema/SemaDiagnostic.h"
21
22
namespace clang {
23
24
1.58M
inline PartialDiagnostic Sema::PDiag(unsigned DiagID) {
25
1.58M
  return PartialDiagnostic(DiagID, Context.getDiagAllocator());
26
1.58M
}
27
28
inline bool
29
5.10M
FTIHasSingleVoidParameter(const DeclaratorChunk::FunctionTypeInfo &FTI) {
30
5.10M
  return FTI.NumParams == 1 && 
!FTI.isVariadic2.20M
&&
31
5.10M
         
FTI.Params[0].Ident == nullptr2.18M
&&
FTI.Params[0].Param895k
&&
32
5.10M
         
cast<ParmVarDecl>(FTI.Params[0].Param)->getType()->isVoidType()895k
;
33
5.10M
}
34
35
inline bool
36
6.09M
FTIHasNonVoidParameters(const DeclaratorChunk::FunctionTypeInfo &FTI) {
37
6.09M
  // Assume FTI is well-formed.
38
6.09M
  return FTI.NumParams && 
!FTIHasSingleVoidParameter(FTI)5.10M
;
39
6.09M
}
40
41
// Helper function to check whether D's attributes match current CUDA mode.
42
// Decls with mismatched attributes and related diagnostics may have to be
43
// ignored during this CUDA compilation pass.
44
57
inline bool DeclAttrsMatchCUDAMode(const LangOptions &LangOpts, Decl *D) {
45
57
  if (!LangOpts.CUDA || 
!D18
)
46
39
    return true;
47
18
  bool isDeviceSideDecl = D->hasAttr<CUDADeviceAttr>() ||
48
18
                          
D->hasAttr<CUDASharedAttr>()10
||
49
18
                          
D->hasAttr<CUDAGlobalAttr>()8
;
50
18
  return isDeviceSideDecl == LangOpts.CUDAIsDevice;
51
18
}
52
53
/// Return a DLL attribute from the declaration.
54
13.1M
inline InheritableAttr *getDLLAttr(Decl *D) {
55
13.1M
  assert(!(D->hasAttr<DLLImportAttr>() && D->hasAttr<DLLExportAttr>()) &&
56
13.1M
         "A declaration cannot be both dllimport and dllexport.");
57
13.1M
  if (auto *Import = D->getAttr<DLLImportAttr>())
58
5.53k
    return Import;
59
13.1M
  if (auto *Export = D->getAttr<DLLExportAttr>())
60
6.45k
    return Export;
61
13.1M
  return nullptr;
62
13.1M
}
63
64
/// Retrieve the depth and index of a template parameter.
65
145k
inline std::pair<unsigned, unsigned> getDepthAndIndex(NamedDecl *ND) {
66
145k
  if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
67
64.0k
    return std::make_pair(TTP->getDepth(), TTP->getIndex());
68
81.5k
69
81.5k
  if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
70
81.3k
    return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
71
185
72
185
  const auto *TTP = cast<TemplateTemplateParmDecl>(ND);
73
185
  return std::make_pair(TTP->getDepth(), TTP->getIndex());
74
185
}
75
76
/// Retrieve the depth and index of an unexpanded parameter pack.
77
inline std::pair<unsigned, unsigned>
78
47.7k
getDepthAndIndex(UnexpandedParameterPack UPP) {
79
47.7k
  if (const auto *TTP = UPP.first.dyn_cast<const TemplateTypeParmType *>())
80
43.3k
    return std::make_pair(TTP->getDepth(), TTP->getIndex());
81
4.40k
82
4.40k
  return getDepthAndIndex(UPP.first.get<NamedDecl *>());
83
4.40k
}
84
85
class TypoCorrectionConsumer : public VisibleDeclConsumer {
86
  typedef SmallVector<TypoCorrection, 1> TypoResultList;
87
  typedef llvm::StringMap<TypoResultList> TypoResultsMap;
88
  typedef std::map<unsigned, TypoResultsMap> TypoEditDistanceMap;
89
90
public:
91
  TypoCorrectionConsumer(Sema &SemaRef,
92
                         const DeclarationNameInfo &TypoName,
93
                         Sema::LookupNameKind LookupKind,
94
                         Scope *S, CXXScopeSpec *SS,
95
                         std::unique_ptr<CorrectionCandidateCallback> CCC,
96
                         DeclContext *MemberContext,
97
                         bool EnteringContext)
98
      : Typo(TypoName.getName().getAsIdentifierInfo()), CurrentTCIndex(0),
99
        SavedTCIndex(0), SemaRef(SemaRef), S(S),
100
        SS(SS ? llvm::make_unique<CXXScopeSpec>(*SS) : nullptr),
101
        CorrectionValidator(std::move(CCC)), MemberContext(MemberContext),
102
        Result(SemaRef, TypoName, LookupKind),
103
        Namespaces(SemaRef.Context, SemaRef.CurContext, SS),
104
4.08k
        EnteringContext(EnteringContext), SearchNamespaces(false) {
105
4.08k
    Result.suppressDiagnostics();
106
4.08k
    // Arrange for ValidatedCorrections[0] to always be an empty correction.
107
4.08k
    ValidatedCorrections.push_back(TypoCorrection());
108
4.08k
  }
109
110
609
  bool includeHiddenDecls() const override { return true; }
111
112
  // Methods for adding potential corrections to the consumer.
113
  void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx,
114
                 bool InBaseClass) override;
115
  void FoundName(StringRef Name);
116
  void addKeywordResult(StringRef Keyword);
117
  void addCorrection(TypoCorrection Correction);
118
119
3.91k
  bool empty() const {
120
3.91k
    return CorrectionResults.empty() && 
ValidatedCorrections.size() == 1112
;
121
3.91k
  }
122
123
  /// Return the list of TypoCorrections for the given identifier from
124
  /// the set of corrections that have the closest edit distance, if any.
125
0
  TypoResultList &operator[](StringRef Name) {
126
0
    return CorrectionResults.begin()->second[Name];
127
0
  }
128
129
  /// Return the edit distance of the corrections that have the
130
  /// closest/best edit distance from the original typop.
131
3.79k
  unsigned getBestEditDistance(bool Normalized) {
132
3.79k
    if (CorrectionResults.empty())
133
0
      return (std::numeric_limits<unsigned>::max)();
134
3.79k
135
3.79k
    unsigned BestED = CorrectionResults.begin()->first;
136
3.79k
    return Normalized ? TypoCorrection::NormalizeEditDistance(BestED) : 
BestED0
;
137
3.79k
  }
138
139
  /// Set-up method to add to the consumer the set of namespaces to use
140
  /// in performing corrections to nested name specifiers. This method also
141
  /// implicitly adds all of the known classes in the current AST context to the
142
  /// to the consumer for correcting nested name specifiers.
143
  void
144
  addNamespaces(const llvm::MapVector<NamespaceDecl *, bool> &KnownNamespaces);
145
146
  /// Return the next typo correction that passes all internal filters
147
  /// and is deemed valid by the consumer's CorrectionCandidateCallback,
148
  /// starting with the corrections that have the closest edit distance. An
149
  /// empty TypoCorrection is returned once no more viable corrections remain
150
  /// in the consumer.
151
  const TypoCorrection &getNextCorrection();
152
153
  /// Get the last correction returned by getNextCorrection().
154
1.50k
  const TypoCorrection &getCurrentCorrection() {
155
1.50k
    return CurrentTCIndex < ValidatedCorrections.size()
156
1.50k
               ? ValidatedCorrections[CurrentTCIndex]
157
1.50k
               : 
ValidatedCorrections[0]0
; // The empty correction.
158
1.50k
  }
159
160
  /// Return the next typo correction like getNextCorrection, but keep
161
  /// the internal state pointed to the current correction (i.e. the next time
162
  /// getNextCorrection is called, it will return the same correction returned
163
  /// by peekNextcorrection).
164
460
  const TypoCorrection &peekNextCorrection() {
165
460
    auto Current = CurrentTCIndex;
166
460
    const TypoCorrection &TC = getNextCorrection();
167
460
    CurrentTCIndex = Current;
168
460
    return TC;
169
460
  }
170
171
  /// Reset the consumer's position in the stream of viable corrections
172
  /// (i.e. getNextCorrection() will return each of the previously returned
173
  /// corrections in order before returning any new corrections).
174
2.26k
  void resetCorrectionStream() {
175
2.26k
    CurrentTCIndex = 0;
176
2.26k
  }
177
178
  /// Return whether the end of the stream of corrections has been
179
  /// reached.
180
1.25k
  bool finished() {
181
1.25k
    return CorrectionResults.empty() &&
182
1.25k
           
CurrentTCIndex >= ValidatedCorrections.size()1.21k
;
183
1.25k
  }
184
185
  /// Save the current position in the correction stream (overwriting any
186
  /// previously saved position).
187
46
  void saveCurrentPosition() {
188
46
    SavedTCIndex = CurrentTCIndex;
189
46
  }
190
191
  /// Restore the saved position in the correction stream.
192
4
  void restoreSavedPosition() {
193
4
    CurrentTCIndex = SavedTCIndex;
194
4
  }
195
196
0
  ASTContext &getContext() const { return SemaRef.Context; }
197
750
  const LookupResult &getLookupResult() const { return Result; }
198
199
64
  bool isAddressOfOperand() const { return CorrectionValidator->IsAddressOfOperand; }
200
375
  const CXXScopeSpec *getSS() const { return SS.get(); }
201
17
  Scope *getScope() const { return S; }
202
4.00k
  CorrectionCandidateCallback *getCorrectionValidator() const {
203
4.00k
    return CorrectionValidator.get();
204
4.00k
  }
205
206
private:
207
  class NamespaceSpecifierSet {
208
    struct SpecifierInfo {
209
      DeclContext* DeclCtx;
210
      NestedNameSpecifier* NameSpecifier;
211
      unsigned EditDistance;
212
    };
213
214
    typedef SmallVector<DeclContext*, 4> DeclContextList;
215
    typedef SmallVector<SpecifierInfo, 16> SpecifierInfoList;
216
217
    ASTContext &Context;
218
    DeclContextList CurContextChain;
219
    std::string CurNameSpecifier;
220
    SmallVector<const IdentifierInfo*, 4> CurContextIdentifiers;
221
    SmallVector<const IdentifierInfo*, 4> CurNameSpecifierIdentifiers;
222
223
    std::map<unsigned, SpecifierInfoList> DistanceMap;
224
225
    /// Helper for building the list of DeclContexts between the current
226
    /// context and the top of the translation unit
227
    static DeclContextList buildContextChain(DeclContext *Start);
228
229
    unsigned buildNestedNameSpecifier(DeclContextList &DeclChain,
230
                                      NestedNameSpecifier *&NNS);
231
232
   public:
233
    NamespaceSpecifierSet(ASTContext &Context, DeclContext *CurContext,
234
                          CXXScopeSpec *CurScopeSpec);
235
236
    /// Add the DeclContext (a namespace or record) to the set, computing
237
    /// the corresponding NestedNameSpecifier and its distance in the process.
238
    void addNameSpecifier(DeclContext *Ctx);
239
240
    /// Provides flat iteration over specifiers, sorted by distance.
241
    class iterator
242
        : public llvm::iterator_facade_base<iterator, std::forward_iterator_tag,
243
                                            SpecifierInfo> {
244
      /// Always points to the last element in the distance map.
245
      const std::map<unsigned, SpecifierInfoList>::iterator OuterBack;
246
      /// Iterator on the distance map.
247
      std::map<unsigned, SpecifierInfoList>::iterator Outer;
248
      /// Iterator on an element in the distance map.
249
      SpecifierInfoList::iterator Inner;
250
251
    public:
252
      iterator(NamespaceSpecifierSet &Set, bool IsAtEnd)
253
          : OuterBack(std::prev(Set.DistanceMap.end())),
254
            Outer(Set.DistanceMap.begin()),
255
6.95k
            Inner(!IsAtEnd ? Outer->second.begin() : OuterBack->second.end()) {
256
6.95k
        assert(!Set.DistanceMap.empty());
257
6.95k
      }
258
259
87.2k
      iterator &operator++() {
260
87.2k
        ++Inner;
261
87.2k
        if (Inner == Outer->second.end() && 
Outer != OuterBack5.96k
) {
262
2.48k
          ++Outer;
263
2.48k
          Inner = Outer->second.begin();
264
2.48k
        }
265
87.2k
        return *this;
266
87.2k
      }
267
268
87.2k
      SpecifierInfo &operator*() { return *Inner; }
269
90.7k
      bool operator==(const iterator &RHS) const { return Inner == RHS.Inner; }
270
    };
271
272
3.47k
    iterator begin() { return iterator(*this, /*IsAtEnd=*/false); }
273
3.47k
    iterator end() { return iterator(*this, /*IsAtEnd=*/true); }
274
  };
275
276
  void addName(StringRef Name, NamedDecl *ND,
277
               NestedNameSpecifier *NNS = nullptr, bool isKeyword = false);
278
279
  /// Find any visible decls for the given typo correction candidate.
280
  /// If none are found, it to the set of candidates for which qualified lookups
281
  /// will be performed to find possible nested name specifier changes.
282
  bool resolveCorrection(TypoCorrection &Candidate);
283
284
  /// Perform qualified lookups on the queued set of typo correction
285
  /// candidates and add the nested name specifier changes to each candidate if
286
  /// a lookup succeeds (at which point the candidate will be returned to the
287
  /// main pool of potential corrections).
288
  void performQualifiedLookups();
289
290
  /// The name written that is a typo in the source.
291
  IdentifierInfo *Typo;
292
293
  /// The results found that have the smallest edit distance
294
  /// found (so far) with the typo name.
295
  ///
296
  /// The pointer value being set to the current DeclContext indicates
297
  /// whether there is a keyword with this name.
298
  TypoEditDistanceMap CorrectionResults;
299
300
  SmallVector<TypoCorrection, 4> ValidatedCorrections;
301
  size_t CurrentTCIndex;
302
  size_t SavedTCIndex;
303
304
  Sema &SemaRef;
305
  Scope *S;
306
  std::unique_ptr<CXXScopeSpec> SS;
307
  std::unique_ptr<CorrectionCandidateCallback> CorrectionValidator;
308
  DeclContext *MemberContext;
309
  LookupResult Result;
310
  NamespaceSpecifierSet Namespaces;
311
  SmallVector<TypoCorrection, 2> QualifiedResults;
312
  bool EnteringContext;
313
  bool SearchNamespaces;
314
};
315
316
1.51k
inline Sema::TypoExprState::TypoExprState() {}
317
318
3.05k
inline Sema::TypoExprState::TypoExprState(TypoExprState &&other) noexcept {
319
3.05k
  *this = std::move(other);
320
3.05k
}
321
322
inline Sema::TypoExprState &Sema::TypoExprState::
323
3.10k
operator=(Sema::TypoExprState &&other) noexcept {
324
3.10k
  Consumer = std::move(other.Consumer);
325
3.10k
  DiagHandler = std::move(other.DiagHandler);
326
3.10k
  RecoveryHandler = std::move(other.RecoveryHandler);
327
3.10k
  return *this;
328
3.10k
}
329
330
} // end namespace clang
331
332
#endif