1 //===--- TypoCorrection.h - Class for typo correction results ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the TypoCorrection class, which stores the results of
11 // Sema's typo correction (Sema::CorrectTypo).
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_SEMA_TYPOCORRECTION_H
16 #define LLVM_CLANG_SEMA_TYPOCORRECTION_H
18 #include "clang/AST/DeclCXX.h"
19 #include "llvm/ADT/SmallVector.h"
23 /// @brief Simple class containing the result of Sema::CorrectTypo
24 class TypoCorrection {
26 TypoCorrection(const DeclarationName &Name, NamedDecl *NameDecl,
27 NestedNameSpecifier *NNS=0, unsigned distance=0)
28 : CorrectionName(Name),
29 CorrectionNameSpec(NNS),
30 EditDistance(distance) {
32 CorrectionDecls.push_back(NameDecl);
35 TypoCorrection(NamedDecl *Name, NestedNameSpecifier *NNS=0,
37 : CorrectionName(Name->getDeclName()),
38 CorrectionNameSpec(NNS),
39 EditDistance(distance) {
41 CorrectionDecls.push_back(Name);
44 TypoCorrection(DeclarationName Name, NestedNameSpecifier *NNS=0,
46 : CorrectionName(Name),
47 CorrectionNameSpec(NNS),
48 EditDistance(distance) {}
51 : CorrectionNameSpec(0), EditDistance(0) {}
53 /// \brief Gets the DeclarationName of the typo correction
54 DeclarationName getCorrection() const { return CorrectionName; }
55 IdentifierInfo* getCorrectionAsIdentifierInfo() const {
56 return CorrectionName.getAsIdentifierInfo();
59 /// \brief Gets the NestedNameSpecifier needed to use the typo correction
60 NestedNameSpecifier* getCorrectionSpecifier() const {
61 return CorrectionNameSpec;
63 void setCorrectionSpecifier(NestedNameSpecifier* NNS) {
64 CorrectionNameSpec = NNS;
67 /// \brief Gets the "edit distance" of the typo correction from the typo
68 unsigned getEditDistance() const { return EditDistance; }
70 /// \brief Gets the pointer to the declaration of the typo correction
71 NamedDecl* getCorrectionDecl() const {
72 return hasCorrectionDecl() ? *(CorrectionDecls.begin()) : 0;
74 template <class DeclClass>
75 DeclClass *getCorrectionDeclAs() const {
76 return dyn_cast_or_null<DeclClass>(getCorrectionDecl());
79 /// \brief Clears the list of NamedDecls before adding the new one.
80 void setCorrectionDecl(NamedDecl *CDecl) {
81 CorrectionDecls.clear();
82 addCorrectionDecl(CDecl);
85 /// \brief Add the given NamedDecl to the list of NamedDecls that are the
86 /// declarations associated with the DeclarationName of this TypoCorrection
87 void addCorrectionDecl(NamedDecl *CDecl);
89 std::string getAsString(const LangOptions &LO) const;
90 std::string getQuoted(const LangOptions &LO) const {
91 return "'" + getAsString(LO) + "'";
94 /// \brief Returns whether this TypoCorrection has a non-empty DeclarationName
95 operator bool() const { return bool(CorrectionName); }
97 /// \brief Mark this TypoCorrection as being a keyword.
98 /// Since addCorrectionDeclsand setCorrectionDecl don't allow NULL to be
99 /// added to the list of the correction's NamedDecl pointers, NULL is added
100 /// as the only element in the list to mark this TypoCorrection as a keyword.
102 CorrectionDecls.clear();
103 CorrectionDecls.push_back(0);
106 // Check if this TypoCorrection is a keyword by checking if the first
107 // item in CorrectionDecls is NULL.
108 bool isKeyword() const {
109 return !CorrectionDecls.empty() &&
110 CorrectionDecls.front() == 0;
113 // Returns true if the correction either is a keyword or has a known decl.
114 bool isResolved() const { return !CorrectionDecls.empty(); }
116 bool isOverloaded() const {
117 return CorrectionDecls.size() > 1;
120 typedef llvm::SmallVector<NamedDecl*, 1>::iterator decl_iterator;
121 decl_iterator begin() {
122 return isKeyword() ? CorrectionDecls.end() : CorrectionDecls.begin();
124 decl_iterator end() { return CorrectionDecls.end(); }
127 bool hasCorrectionDecl() const {
128 return (!isKeyword() && !CorrectionDecls.empty());
132 DeclarationName CorrectionName;
133 NestedNameSpecifier *CorrectionNameSpec;
134 llvm::SmallVector<NamedDecl*, 1> CorrectionDecls;
135 unsigned EditDistance;