Skip to content

Source · fairmq/tidy/

ModernizeNonNamespacedTypes.hheader

Described in Check code with fairmq-tidy, fairmq-tidy.

fairmq/tidy/ModernizeNonNamespacedTypes.h73 lines · C++GitHub ↗
1/********************************************************************************2 *    Copyright (C) 2021 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH    *3 *                                                                              *4 *              This software is distributed under the terms of the             *5 *              GNU Lesser General Public Licence (LGPL) version 3,             *6 *                  copied verbatim in the file "LICENSE"                       *7 ********************************************************************************/8 9#ifndef FAIR_MQ_TIDY_MODERNIZENONNAMESPACEDTYPES10#define FAIR_MQ_TIDY_MODERNIZENONNAMESPACEDTYPES11 12#include <clang/AST/AST.h>13#include <clang/ASTMatchers/ASTMatchFinder.h>14#include <clang/ASTMatchers/ASTMatchers.h>15#include <clang/Basic/Diagnostic.h>16#include <llvm/Support/Casting.h>17 18namespace fair::mq::tidy {19 20struct ModernizeNonNamespacedTypes21{22    ModernizeNonNamespacedTypes() = delete;23    ModernizeNonNamespacedTypes(clang::ast_matchers::MatchFinder& finder)24    {25        using namespace clang::ast_matchers;26 27        // https://clang.llvm.org/docs/LibASTMatchersReference.html28        finder.addMatcher(29            typeLoc(loc(qualType(hasDeclaration(namedDecl(matchesName("::FairMQ")).bind("decl")))))30                .bind("loc"),31            &fCallback);32    }33 34    struct Callback : clang::ast_matchers::MatchFinder::MatchCallback35    {36        auto run(clang::ast_matchers::MatchFinder::MatchResult const& m) -> void final37        {38            using namespace clang;39 40            auto const type_loc(m.Nodes.getNodeAs<TypeLoc>("loc"));41            auto const named_decl(m.Nodes.getNodeAs<NamedDecl>("decl"));42 43            if (auto const type_alias_decl = m.Nodes.getNodeAs<TypeAliasDecl>("decl")) {44                auto const underlying_type(type_alias_decl->getUnderlyingType());45 46                if (underlying_type.getAsString().rfind("fair::mq::", 0) == 0) {47                    auto& diag_engine(m.Context->getDiagnostics());48                    auto const location(type_loc->getBeginLoc());49                    if (location.isValid()) {50                        auto builder(diag_engine.Report(51                            location,52                            diag_engine.getCustomDiagID(53                                DiagnosticsEngine::Warning,54                                "Modernize non-namespaced type %0 with %1. [%2]")));55                        builder << named_decl;56                        builder << underlying_type;57                        builder << "fairmq-modernize-nonnamespaced-types";58 59                        builder.AddFixItHint(FixItHint::CreateReplacement(60                            type_loc->getSourceRange(), underlying_type.getAsString()));61                    }62                }63            }64        }65    };66 67  private:68    Callback fCallback;69};70 71}   // namespace fair::mq::tidy72 73#endif /* FAIR_MQ_TIDY_MODERNIZENONNAMESPACEDTYPES */