[clang-tidy] Avoid C arrays check
Summary:
[[ https://bugs.llvm.org/show_bug.cgi?id=39224 | PR39224 ]]
As discussed, we can't always do the transform automatically due to that array-to-pointer decay of C array.
In order to detect whether we can do said transform, we'd need to be able to see all usages of said array,
which is, i would say, rather impossible if e.g. it is in the header.
Thus right now no fixit exists.
Exceptions: `extern "C"` code.
References:
* [[ https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es27-use-stdarray-or-stack_array-for-arrays-on-the-stack | CPPCG ES.27: Use std::array or stack_array for arrays on the stack ]]
* [[ https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#slcon1-prefer-using-stl-array-or-vector-instead-of-a-c-array | CPPCG SL.con.1: Prefer using STL array or vector instead of a C array ]]
* HICPP `4.1.1 Ensure that a function argument does not undergo an array-to-pointer conversion`
* MISRA `5-2-12 An identifier with array type passed as a function argument shall not decay to a pointer`
Reviewers: aaron.ballman, JonasToth, alexfh, hokein, xazax.hun
Reviewed By: JonasToth
Subscribers: Eugene.Zelenko, mgorny, rnkovacs, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D53771
llvm-svn: 346835
2018-11-14 17:01:08 +08:00
|
|
|
//===--- AvoidCArraysCheck.cpp - clang-tidy -------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
[clang-tidy] Avoid C arrays check
Summary:
[[ https://bugs.llvm.org/show_bug.cgi?id=39224 | PR39224 ]]
As discussed, we can't always do the transform automatically due to that array-to-pointer decay of C array.
In order to detect whether we can do said transform, we'd need to be able to see all usages of said array,
which is, i would say, rather impossible if e.g. it is in the header.
Thus right now no fixit exists.
Exceptions: `extern "C"` code.
References:
* [[ https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es27-use-stdarray-or-stack_array-for-arrays-on-the-stack | CPPCG ES.27: Use std::array or stack_array for arrays on the stack ]]
* [[ https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#slcon1-prefer-using-stl-array-or-vector-instead-of-a-c-array | CPPCG SL.con.1: Prefer using STL array or vector instead of a C array ]]
* HICPP `4.1.1 Ensure that a function argument does not undergo an array-to-pointer conversion`
* MISRA `5-2-12 An identifier with array type passed as a function argument shall not decay to a pointer`
Reviewers: aaron.ballman, JonasToth, alexfh, hokein, xazax.hun
Reviewed By: JonasToth
Subscribers: Eugene.Zelenko, mgorny, rnkovacs, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D53771
llvm-svn: 346835
2018-11-14 17:01:08 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "AvoidCArraysCheck.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
AST_MATCHER(clang::TypeLoc, hasValidBeginLoc) {
|
|
|
|
return Node.getBeginLoc().isValid();
|
|
|
|
}
|
|
|
|
|
|
|
|
AST_MATCHER_P(clang::TypeLoc, hasType,
|
|
|
|
clang::ast_matchers::internal::Matcher<clang::Type>,
|
|
|
|
InnerMatcher) {
|
|
|
|
const clang::Type *TypeNode = Node.getTypePtr();
|
|
|
|
return TypeNode != nullptr &&
|
|
|
|
InnerMatcher.matches(*TypeNode, Finder, Builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
AST_MATCHER(clang::RecordDecl, isExternCContext) {
|
|
|
|
return Node.isExternCContext();
|
|
|
|
}
|
|
|
|
|
2019-02-07 03:17:30 +08:00
|
|
|
AST_MATCHER(clang::ParmVarDecl, isArgvOfMain) {
|
|
|
|
const clang::DeclContext *DC = Node.getDeclContext();
|
|
|
|
const auto *FD = llvm::dyn_cast<clang::FunctionDecl>(DC);
|
|
|
|
return FD ? FD->isMain() : false;
|
|
|
|
}
|
|
|
|
|
[clang-tidy] Avoid C arrays check
Summary:
[[ https://bugs.llvm.org/show_bug.cgi?id=39224 | PR39224 ]]
As discussed, we can't always do the transform automatically due to that array-to-pointer decay of C array.
In order to detect whether we can do said transform, we'd need to be able to see all usages of said array,
which is, i would say, rather impossible if e.g. it is in the header.
Thus right now no fixit exists.
Exceptions: `extern "C"` code.
References:
* [[ https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es27-use-stdarray-or-stack_array-for-arrays-on-the-stack | CPPCG ES.27: Use std::array or stack_array for arrays on the stack ]]
* [[ https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#slcon1-prefer-using-stl-array-or-vector-instead-of-a-c-array | CPPCG SL.con.1: Prefer using STL array or vector instead of a C array ]]
* HICPP `4.1.1 Ensure that a function argument does not undergo an array-to-pointer conversion`
* MISRA `5-2-12 An identifier with array type passed as a function argument shall not decay to a pointer`
Reviewers: aaron.ballman, JonasToth, alexfh, hokein, xazax.hun
Reviewed By: JonasToth
Subscribers: Eugene.Zelenko, mgorny, rnkovacs, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D53771
llvm-svn: 346835
2018-11-14 17:01:08 +08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace modernize {
|
|
|
|
|
|
|
|
void AvoidCArraysCheck::registerMatchers(MatchFinder *Finder) {
|
|
|
|
// std::array<> is avaliable since C++11.
|
|
|
|
if (!getLangOpts().CPlusPlus11)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Finder->addMatcher(
|
|
|
|
typeLoc(hasValidBeginLoc(), hasType(arrayType()),
|
2019-02-07 03:17:30 +08:00
|
|
|
unless(anyOf(hasParent(parmVarDecl(isArgvOfMain())),
|
|
|
|
hasParent(varDecl(isExternC())),
|
[clang-tidy] Avoid C arrays check
Summary:
[[ https://bugs.llvm.org/show_bug.cgi?id=39224 | PR39224 ]]
As discussed, we can't always do the transform automatically due to that array-to-pointer decay of C array.
In order to detect whether we can do said transform, we'd need to be able to see all usages of said array,
which is, i would say, rather impossible if e.g. it is in the header.
Thus right now no fixit exists.
Exceptions: `extern "C"` code.
References:
* [[ https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es27-use-stdarray-or-stack_array-for-arrays-on-the-stack | CPPCG ES.27: Use std::array or stack_array for arrays on the stack ]]
* [[ https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#slcon1-prefer-using-stl-array-or-vector-instead-of-a-c-array | CPPCG SL.con.1: Prefer using STL array or vector instead of a C array ]]
* HICPP `4.1.1 Ensure that a function argument does not undergo an array-to-pointer conversion`
* MISRA `5-2-12 An identifier with array type passed as a function argument shall not decay to a pointer`
Reviewers: aaron.ballman, JonasToth, alexfh, hokein, xazax.hun
Reviewed By: JonasToth
Subscribers: Eugene.Zelenko, mgorny, rnkovacs, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D53771
llvm-svn: 346835
2018-11-14 17:01:08 +08:00
|
|
|
hasParent(fieldDecl(
|
|
|
|
hasParent(recordDecl(isExternCContext())))),
|
|
|
|
hasAncestor(functionDecl(isExternC())))))
|
|
|
|
.bind("typeloc"),
|
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AvoidCArraysCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
const auto *ArrayType = Result.Nodes.getNodeAs<TypeLoc>("typeloc");
|
|
|
|
|
|
|
|
static constexpr llvm::StringLiteral UseArray = llvm::StringLiteral(
|
|
|
|
"do not declare C-style arrays, use std::array<> instead");
|
|
|
|
static constexpr llvm::StringLiteral UseVector = llvm::StringLiteral(
|
|
|
|
"do not declare C VLA arrays, use std::vector<> instead");
|
|
|
|
|
|
|
|
diag(ArrayType->getBeginLoc(),
|
|
|
|
ArrayType->getTypePtr()->isVariableArrayType() ? UseVector : UseArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace modernize
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|