aboutsummaryrefslogtreecommitdiff
path: root/include/clang/Sema/TypoCorrection.h
blob: 9965953538a1140f3cf1c53356dff61dd2a22019 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//===--- TypoCorrection.h - Class for typo correction results ---*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the TypoCorrection class, which stores the results of
// Sema's typo correction (Sema::CorrectTypo).
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_SEMA_TYPOCORRECTION_H
#define LLVM_CLANG_SEMA_TYPOCORRECTION_H

#include "clang/AST/DeclCXX.h"

namespace clang {

/// @brief Simple class containing the result of Sema::CorrectTypo
class TypoCorrection {
public:
  TypoCorrection(const DeclarationName &Name, NamedDecl *NameDecl,
                 NestedNameSpecifier *NNS=NULL, unsigned distance=0)
      : CorrectionName(Name),
        CorrectionNameSpec(NNS),
        CorrectionDecl(NameDecl),
        EditDistance(distance) {}

  TypoCorrection(NamedDecl *Name, NestedNameSpecifier *NNS=NULL,
                 unsigned distance=0)
      : CorrectionName(Name->getDeclName()),
        CorrectionNameSpec(NNS),
        CorrectionDecl(Name),
        EditDistance(distance)  {}

  TypoCorrection(DeclarationName Name, NestedNameSpecifier *NNS=NULL,
                 unsigned distance=0)
      : CorrectionName(Name),
        CorrectionNameSpec(NNS),
        CorrectionDecl(NULL),
        EditDistance(distance)  {}

  TypoCorrection()
      : CorrectionName(), CorrectionNameSpec(NULL), CorrectionDecl(NULL),
        EditDistance(0) {}

  /// \brief Gets the DeclarationName of the typo correction
  DeclarationName getCorrection() const { return CorrectionName; }
  IdentifierInfo* getCorrectionAsIdentifierInfo() const {
    return CorrectionName.getAsIdentifierInfo();
  }

  /// \brief Gets the NestedNameSpecifier needed to use the typo correction
  NestedNameSpecifier* getCorrectionSpecifier() const {
    return CorrectionNameSpec;
  }
  void setCorrectionSpecifier(NestedNameSpecifier* NNS) {
    CorrectionNameSpec = NNS;
  }

  /// \brief Gets the "edit distance" of the typo correction from the typo
  unsigned getEditDistance() const { return EditDistance; }

  /// \brief Gets the pointer to the declaration of the typo correction
  NamedDecl* getCorrectionDecl() const {
    return isKeyword() ? NULL : CorrectionDecl;
  }
  template <class DeclClass>
  DeclClass *getCorrectionDeclAs() const {
    return dyn_cast_or_null<DeclClass>(getCorrectionDecl());
  }
  
  void setCorrectionDecl(NamedDecl *CDecl) {
    CorrectionDecl = CDecl;
    if (!CorrectionName)
      CorrectionName = CDecl->getDeclName();
  }

  std::string getAsString(const LangOptions &LO) const;
  std::string getQuoted(const LangOptions &LO) const {
    return "'" + getAsString(LO) + "'";
  }

  operator bool() const { return bool(CorrectionName); }

  static inline NamedDecl *KeywordDecl() { return (NamedDecl*)-1; }
  bool isKeyword() const { return CorrectionDecl == KeywordDecl(); }

  // Returns true if the correction either is a keyword or has a known decl.
  bool isResolved() const { return CorrectionDecl != NULL; }

private:
  // Results.
  DeclarationName CorrectionName;
  NestedNameSpecifier *CorrectionNameSpec;
  NamedDecl *CorrectionDecl;
  unsigned EditDistance;
};

}

#endif