-
Notifications
You must be signed in to change notification settings - Fork 0
/
NonConstCharPtrArgCheck.cpp
49 lines (42 loc) · 1.64 KB
/
NonConstCharPtrArgCheck.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//===--- NonConstCharPtrArgCheck.cpp - clang-tidy--------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "NonConstCharPtrArgCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include <iostream>
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace valpo {
void NonConstCharPtrArgCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(functionDecl().bind("function"), this);
}
void NonConstCharPtrArgCheck::check(const MatchFinder::MatchResult &Result) {
const auto func = Result.Nodes.getNodeAs<FunctionDecl>("function");
if (func) {
for (const auto& p : func->parameters()) {
const auto& t = p->getType(); // QualType
auto typeName = t.getAsString();
auto typePtr = t.getTypePtrOrNull();
if (typeName.find("char") != std::string::npos && typePtr && typePtr->isPointerType()) {
auto sType{t};
while (sType->isPointerType()) sType = sType->getPointeeType();
if (!sType.isConstQualified()) {
//diag(func->getLocStart(), "scalar type: %0") << sType;
diag(func->getLocStart(), "function taking non-const char pointer parameter");
}
}
}
}
else
diag(SourceLocation(), "no method");
}
} // namespace valpo
} // namespace tidy
} // namespace clang