File indexing completed on 2024-04-28 05:38:32

0001 /*
0002     SPDX-FileCopyrightText: 2019 Jean-Michaƫl Celerier <jeanmichael.celerier@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef CLAZY_QPROPERTY_TYPE_MISMATCH_H
0008 #define CLAZY_QPROPERTY_TYPE_MISMATCH_H
0009 
0010 #include "checkbase.h"
0011 
0012 #include <clang/Basic/SourceLocation.h>
0013 
0014 #include <string>
0015 #include <unordered_map>
0016 #include <vector>
0017 
0018 class ClazyContext;
0019 namespace clang
0020 {
0021 class CXXMethodDecl;
0022 class FieldDecl;
0023 class Decl;
0024 class MacroInfo;
0025 class Token;
0026 class TypeAliasDecl;
0027 } // namespace clang
0028 
0029 /**
0030  * See README-qproperty-type-mismatch.md for more info.
0031  */
0032 class QPropertyTypeMismatch : public CheckBase
0033 {
0034 public:
0035     explicit QPropertyTypeMismatch(const std::string &name, ClazyContext *context);
0036     void VisitDecl(clang::Decl *) override;
0037 
0038 private:
0039     void VisitMethod(const clang::CXXMethodDecl &);
0040     void VisitField(const clang::FieldDecl &);
0041     void VisitTypedef(const clang::TypedefNameDecl *);
0042 
0043     void VisitMacroExpands(const clang::Token &MacroNameTok, const clang::SourceRange &range, const clang::MacroInfo *minfo = nullptr) override;
0044 
0045     struct Property {
0046         clang::SourceLocation loc;
0047         bool member{};
0048         std::string name;
0049         std::string type;
0050         std::string read;
0051         std::string write;
0052         std::string notify;
0053     };
0054 
0055     std::vector<Property> m_qproperties;
0056     std::string cleanupType(clang::QualType type, bool unscoped = false) const;
0057     void checkMethodAgainstProperty(const Property &prop, const clang::CXXMethodDecl &method, const std::string &methodName);
0058     void checkFieldAgainstProperty(const Property &prop, const clang::FieldDecl &method, const std::string &methodName);
0059 
0060     bool typesMatch(const std::string &type1, clang::QualType type2Qt, std::string &type2Cleaned) const;
0061     std::unordered_map<std::string, clang::QualType> m_typedefMap;
0062 };
0063 
0064 #endif