TypoCorrection.h revision d8bba9c15230d2b1b3893e272106aa79efc50251
1//===--- TypoCorrection.h - Class for typo correction results ---*- 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 defines the TypoCorrection class, which stores the results of
11// Sema's typo correction (Sema::CorrectTypo).
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_SEMA_TYPOCORRECTION_H
16#define LLVM_CLANG_SEMA_TYPOCORRECTION_H
17
18#include "clang/AST/DeclCXX.h"
19
20namespace clang {
21
22/// @brief Simple class containing the result of Sema::CorrectTypo
23class TypoCorrection {
24public:
25  TypoCorrection(const DeclarationName &Name, NamedDecl *NameDecl,
26                 NestedNameSpecifier *NNS=NULL, unsigned distance=0)
27      : CorrectionName(Name),
28        CorrectionNameSpec(NNS),
29        CorrectionDecl(NameDecl),
30        EditDistance(distance) {}
31
32  TypoCorrection(NamedDecl *Name, NestedNameSpecifier *NNS=NULL,
33                 unsigned distance=0)
34      : CorrectionName(Name->getDeclName()),
35        CorrectionNameSpec(NNS),
36        CorrectionDecl(Name),
37        EditDistance(distance)  {}
38
39  TypoCorrection(DeclarationName Name, NestedNameSpecifier *NNS=NULL,
40                 unsigned distance=0)
41      : CorrectionName(Name),
42        CorrectionNameSpec(NNS),
43        CorrectionDecl(NULL),
44        EditDistance(distance)  {}
45
46  TypoCorrection()
47      : CorrectionName(), CorrectionNameSpec(NULL), CorrectionDecl(NULL),
48        EditDistance(0) {}
49
50  /// \brief Gets the DeclarationName of the typo correction
51  DeclarationName getCorrection() const { return CorrectionName; }
52  IdentifierInfo* getCorrectionAsIdentifierInfo() const {
53    return CorrectionName.getAsIdentifierInfo();
54  }
55
56  /// \brief Gets the NestedNameSpecifier needed to use the typo correction
57  NestedNameSpecifier* getCorrectionSpecifier() const {
58    return CorrectionNameSpec;
59  }
60  void setCorrectionSpecifier(NestedNameSpecifier* NNS) {
61    CorrectionNameSpec = NNS;
62  }
63
64  /// \brief Gets the "edit distance" of the typo correction from the typo
65  unsigned getEditDistance() const { return EditDistance; }
66
67  /// \brief Gets the pointer to the declaration of the typo correction
68  NamedDecl* getCorrectionDecl() const {
69    return isKeyword() ? NULL : CorrectionDecl;
70  }
71  template <class DeclClass>
72  DeclClass *getCorrectionDeclAs() const {
73    return dyn_cast_or_null<DeclClass>(getCorrectionDecl());
74  }
75
76  void setCorrectionDecl(NamedDecl *CDecl) {
77    CorrectionDecl = CDecl;
78    if (!CorrectionName)
79      CorrectionName = CDecl->getDeclName();
80  }
81
82  std::string getAsString(const LangOptions &LO) const;
83  std::string getQuoted(const LangOptions &LO) const {
84    return "'" + getAsString(LO) + "'";
85  }
86
87  operator bool() const { return bool(CorrectionName); }
88
89  static inline NamedDecl *KeywordDecl() { return (NamedDecl*)-1; }
90  bool isKeyword() const { return CorrectionDecl == KeywordDecl(); }
91
92  // Returns true if the correction either is a keyword or has a known decl.
93  bool isResolved() const { return CorrectionDecl != NULL; }
94
95private:
96  // Results.
97  DeclarationName CorrectionName;
98  NestedNameSpecifier *CorrectionNameSpec;
99  NamedDecl *CorrectionDecl;
100  unsigned EditDistance;
101};
102
103}
104
105#endif
106