Files
carbon-lang/migrate_cpp/cpp_refactoring/fn_inserter.cpp
T
Jon Ross-PerkinsandRichard Smith 3f9a06aee3 Look at flipping clang-tidy's misc-* to enable-by-default (#4699)
I was wondering, instead of treating `misc` differently and enabling
specific checks, maybe we can flip that since we actually seem okay with
most of the checks?

The main check I'm enabling, with significant edits here, is
`misc-no-recursion`. But maybe this is helpful to enable, even with the
necessary NOLINTs, since we want to avoid recursion in the toolchain?
This PR shows some example fixes in subst.cpp (which are more stylistic,
since the code shouldn't actually have recursed due to its structure; I
think we could remove the warning on TryResolveInst the same way). Some
also just don't seem worth fixing, like those in tests files (I didn't
see a way to exclude files in .clang-tidy, so instead I'm using
NOLINTBEGIN). But I think we might actually want to fix inst_namer, and
there's enough in convert that I didn't look closely.

Also, I made some protected -> private style fixes based on
`misc-non-private-member-variables-in-classes` (this is also how I
noticed `class Real` versus `struct Real`). With node_stack, it looks
like the `protected` wasn't even used. [Per
style](https://google.github.io/styleguide/cppguide.html#Access_Control),
data members should be private outside tests. But since we can't
trivially exclude `protected` members in tests, I'm turning it off -- I
don't view it as offering enough benefit on the whole.

migrate_cpp issues are preexisting (I believe we just aren't monitoring
it), but changes there make `bazel build --config=clang-tidy -k //...`
work cleanly.

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2024-12-17 21:20:37 +00:00

51 lines
1.8 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "migrate_cpp/cpp_refactoring/fn_inserter.h"
#include "clang/ASTMatchers/ASTMatchers.h"
// NOLINTNEXTLINE(readability-identifier-naming)
namespace cam = ::clang::ast_matchers;
namespace Carbon {
static constexpr char Label[] = "FnInserter";
void FnInserter::Run() {
const auto& decl = GetNodeAsOrDie<clang::FunctionDecl>(Label);
// For names like "Class::Method", replace up to "Class" not "Method".
clang::NestedNameSpecifierLoc qual_loc = decl.getQualifierLoc();
clang::SourceLocation name_begin_loc =
qual_loc.hasQualifier() ? qual_loc.getBeginLoc() : decl.getLocation();
auto range =
clang::CharSourceRange::getCharRange(decl.getBeginLoc(), name_begin_loc);
// In order to handle keywords like "virtual" in "virtual auto Foo() -> ...",
// scan the replaced text and only drop auto/void entries.
llvm::SmallVector<llvm::StringRef> split;
GetSourceText(range).split(split, ' ', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
std::string new_text = "fn ";
for (llvm::StringRef t : split) {
if (t != "auto" && t != "void") {
new_text += t.str() + " ";
}
}
AddReplacement(range, new_text);
}
void FnInserterFactory::AddMatcher(cam::MatchFinder* finder,
cam::MatchFinder::MatchCallback* callback) {
finder->addMatcher(
cam::functionDecl(cam::anyOf(cam::hasTrailingReturn(),
cam::returns(cam::asString("void"))),
cam::unless(cam::anyOf(cam::cxxConstructorDecl(),
cam::cxxDestructorDecl())))
.bind(Label),
callback);
}
} // namespace Carbon