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"
22 /// @brief Simple class containing the result of Sema::CorrectTypo
23 class TypoCorrection {
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) {}
32 TypoCorrection(NamedDecl *Name, NestedNameSpecifier *NNS=NULL,
34 : CorrectionName(Name->getDeclName()),
35 CorrectionNameSpec(NNS),
37 EditDistance(distance) {}
39 TypoCorrection(DeclarationName Name, NestedNameSpecifier *NNS=NULL,
41 : CorrectionName(Name),
42 CorrectionNameSpec(NNS),
44 EditDistance(distance) {}
47 : CorrectionName(), CorrectionNameSpec(NULL), CorrectionDecl(NULL),
50 /// \brief Gets the DeclarationName of the typo correction
51 DeclarationName getCorrection() const { return CorrectionName; }
52 IdentifierInfo* getCorrectionAsIdentifierInfo() const {
53 return CorrectionName.getAsIdentifierInfo();
56 /// \brief Gets the NestedNameSpecifier needed to use the typo correction
57 NestedNameSpecifier* getCorrectionSpecifier() const {
58 return CorrectionNameSpec;
60 void setCorrectionSpecifier(NestedNameSpecifier* NNS) {
61 CorrectionNameSpec = NNS;
64 /// \brief Gets the "edit distance" of the typo correction from the typo
65 unsigned getEditDistance() const { return EditDistance; }
67 /// \brief Gets the pointer to the declaration of the typo correction
68 NamedDecl* getCorrectionDecl() const {
69 return isKeyword() ? NULL : CorrectionDecl;
71 template <class DeclClass>
72 DeclClass *getCorrectionDeclAs() const {
73 return dyn_cast_or_null<DeclClass>(getCorrectionDecl());
76 void setCorrectionDecl(NamedDecl *CDecl) {
77 CorrectionDecl = CDecl;
79 CorrectionName = CDecl->getDeclName();
82 std::string getAsString(const LangOptions &LO) const;
83 std::string getQuoted(const LangOptions &LO) const {
84 return "'" + getAsString(LO) + "'";
87 operator bool() const { return bool(CorrectionName); }
89 static inline NamedDecl *KeywordDecl() { return (NamedDecl*)-1; }
90 bool isKeyword() const { return CorrectionDecl == KeywordDecl(); }
92 // Returns true if the correction either is a keyword or has a known decl.
93 bool isResolved() const { return CorrectionDecl != NULL; }
97 DeclarationName CorrectionName;
98 NestedNameSpecifier *CorrectionNameSpec;
99 NamedDecl *CorrectionDecl;
100 unsigned EditDistance;