clang-tools 24.0.0git
AvoidPragmaCommentCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "clang/ASTMatchers/ASTMatchFinder.h"
11
12#include <string>
13
14using namespace clang::ast_matchers;
15
17
18static const internal::VariadicDynCastAllOfMatcher<Decl, PragmaCommentDecl>
19 // All other node matchers declared in this way are camelCase
20 // NOLINTNEXTLINE(readability-identifier-naming)
22
24 Finder->addMatcher(pragmaCommentDecl().bind("pragma"), this);
25}
26
27void AvoidPragmaCommentCheck::check(const MatchFinder::MatchResult &Result) {
28 const auto *Pragma = Result.Nodes.getNodeAs<PragmaCommentDecl>("pragma");
29
30 std::string Msg{"avoid 'pragma comment' directive"};
31
32 // We can give specific advice about comments that add linker flags, but other
33 // kinds are too generic
34 const PragmaMSCommentKind &Kind = Pragma->getCommentKind();
35 switch (Kind) {
36 case PragmaMSCommentKind::PCK_Lib:
37 Msg.append("; use the build system to link libraries");
38 break;
39 case PragmaMSCommentKind::PCK_Linker:
40 Msg.append("; use the build system to set linker options");
41 break;
42 case PragmaMSCommentKind::PCK_Unknown:
43 llvm_unreachable("unexpected pragma comment kind");
44 default:
45 break;
46 }
47 diag(Pragma->getBeginLoc(), Msg);
48}
49
50} // namespace clang::tidy::portability
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
static const internal::VariadicDynCastAllOfMatcher< Decl, PragmaCommentDecl > pragmaCommentDecl