mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Note, not trying to address every last error, just some obvious/easy ones.
```
/usr/local/google/home/jperkins/dev/carbon-lang/common/string_helpers.cpp:200:13: warning: prefer transparent functors 'less_equal<>' [modernize-use-transparent-functors]
auto le = std::less_equal<const char*>();
^
/usr/local/google/home/jperkins/dev/carbon-lang/toolchain/semantics/nodes/function.h:22:45: warning: pass by value and use std::move [modernize-pass-by-value]
Function(ParseTree::Node node, NodeId id, llvm::SmallVector<NodeRef> body)
^
/usr/local/google/home/jperkins/dev/carbon-lang/explorer/ast/declaration.cpp:230:14: warning: static member accessed through instance [readability-static-accessed-through-instance]
return cast<SelfDeclaration>(declaration).name();
^
/usr/local/google/home/jperkins/dev/carbon-lang/explorer/interpreter/type_checker.cpp:1104:33: warning: std::move of the variable 'impl' of the trivially-copyable type 'ConstraintType::ImplConstraint' has no effect [performance-move-const-arg]
impl_constraints_.push_back(std::move(impl));
^
/usr/local/google/home/jperkins/dev/carbon-lang/explorer/interpreter/type_checker.cpp:1145:36: warning: std::move of the variable 'rewrite' of the trivially-copyable type 'ConstraintType::RewriteConstraint' has no effect [performance-move-const-arg]
rewrite_constraints_.push_back(std::move(rewrite));
^
/usr/local/google/home/jperkins/dev/carbon-lang/explorer/interpreter/type_checker.cpp:1156:32: warning: std::move of the variable 'context' of the trivially-copyable type 'ConstraintType::LookupContext' has no effect [performance-move-const-arg]
lookup_contexts_.push_back(std::move(context));
^
/usr/local/google/home/jperkins/dev/carbon-lang/explorer/fuzzing/fuzzverter.cpp:74:42: warning: argument name 'trace' in comment does not match parameter name 'parser_debug' [bugprone-argument-comment]
/*trace=*/false);
^
./explorer/syntax/parse.h:19:17: note: 'parser_debug' declared here
bool parser_debug) -> ErrorOr<Carbon::AST>;
^
/usr/local/google/home/jperkins/dev/carbon-lang/explorer/fuzzing/proto_to_carbon_test.cpp:34:55: warning: argument name 'trace' in comment does not match parameter name 'parser_debug' [bugprone-argument-comment]
const ErrorOr<AST> ast = Carbon::Parse(&arena, f, /*trace=*/false);
^
./explorer/syntax/parse.h:19:17: note: 'parser_debug' declared here
bool parser_debug) -> ErrorOr<Carbon::AST>;
^
/usr/local/google/home/jperkins/dev/carbon-lang/explorer/fuzzing/proto_to_carbon_test.cpp:42:41: warning: argument name 'trace' in comment does not match parameter name 'parser_debug' [bugprone-argument-comment]
&arena, f, source_from_proto, /*trace=*/false);
^
./explorer/syntax/parse.h:25:59: note: 'parser_debug' declared here
std::string_view file_contents, bool parser_debug)
^
/usr/local/google/home/jperkins/dev/carbon-lang/explorer/syntax/parse_test.cpp:27:60: warning: argument name 'trace' in comment does not match parameter name 'parser_debug' [bugprone-argument-comment]
ParseFromString(&arena, "file.carbon", FileContents, /*trace=*/false);
^
./explorer/syntax/parse.h:25:59: note: 'parser_debug' declared here
std::string_view file_contents, bool parser_debug)
^
/usr/local/google/home/jperkins/dev/carbon-lang/migrate_cpp/cpp_refactoring/var_decl.cpp:57:58: warning: string concatenation results in allocation of unnecessary temporary strings; consider using 'operator+=' or 'string::append()' instead [performance-inefficient-string-concatenation]
segments.push_back({type_loc_class, qual_str + " " + range_str});
^
/usr/local/google/home/jperkins/dev/carbon-lang/explorer/fuzzing/ast_to_proto_test.cpp:105:55: warning: argument name 'trace' in comment does not match parameter name 'parser_debug' [bugprone-argument-comment]
const ErrorOr<AST> ast = Carbon::Parse(&arena, f, /*trace=*/false);
^
./explorer/syntax/parse.h:19:17: note: 'parser_debug' declared here
bool parser_debug) -> ErrorOr<Carbon::AST>;
^
```
145 lines
5.3 KiB
C++
145 lines
5.3 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/var_decl.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
|
#include "llvm/Support/FormatVariadic.h"
|
|
|
|
namespace cam = ::clang::ast_matchers;
|
|
|
|
namespace Carbon {
|
|
|
|
static constexpr char Label[] = "VarDecl";
|
|
|
|
// Helper function for printing TypeLocClass. Useful for debugging.
|
|
LLVM_ATTRIBUTE_UNUSED
|
|
static auto TypeLocClassToString(clang::TypeLoc::TypeLocClass c)
|
|
-> std::string {
|
|
switch (c) {
|
|
// Mirrors the definition in clang/AST/TypeLoc.h in order to print names.
|
|
#define ABSTRACT_TYPE(Class, Base)
|
|
#define TYPE(Class, Base) \
|
|
case clang::TypeLoc::Class: \
|
|
return #Class;
|
|
#include "clang/AST/TypeNodes.inc"
|
|
case clang::TypeLoc::Qualified:
|
|
return "Qualified";
|
|
}
|
|
}
|
|
|
|
// Returns a string for the type.
|
|
auto VarDecl::GetTypeStr(const clang::VarDecl& decl) -> std::string {
|
|
// Built a vector of class information, because we'll be traversing reverse
|
|
// order to construct the final type.
|
|
auto type_loc = decl.getTypeSourceInfo()->getTypeLoc();
|
|
std::vector<std::pair<clang::TypeLoc::TypeLocClass, std::string>> segments;
|
|
while (!type_loc.isNull()) {
|
|
std::string text;
|
|
auto qualifiers = type_loc.getType().getLocalQualifiers();
|
|
std::string qual_str;
|
|
if (!qualifiers.empty()) {
|
|
qual_str = qualifiers.getAsString();
|
|
}
|
|
auto range =
|
|
clang::CharSourceRange::getTokenRange(type_loc.getLocalSourceRange());
|
|
std::string range_str = GetSourceText(range).str();
|
|
|
|
// Make a list of segments with their TypeLocClass for reconstruction of the
|
|
// string. Locally, we will have a qualifier (such as `const`) and a type
|
|
// string (such as `int`) which is also used.
|
|
auto type_loc_class = type_loc.getTypeLocClass();
|
|
if (qual_str.empty()) {
|
|
segments.push_back({type_loc_class, range_str});
|
|
} else if (range_str.empty()) {
|
|
segments.push_back({type_loc_class, qual_str});
|
|
} else {
|
|
segments.push_back(
|
|
{type_loc_class, llvm::formatv("{0} {1}", qual_str, range_str)});
|
|
}
|
|
|
|
type_loc = type_loc.getNextTypeLoc();
|
|
}
|
|
|
|
// Construct the final type based on the class of each step. This reverses to
|
|
// start from the "inside" of the type and go "out" when constructing
|
|
// type_str.
|
|
std::string type_str;
|
|
auto prev_class = clang::TypeLoc::Auto; // Placeholder class, used in loop.
|
|
for (const auto& [type_loc_class, text] : llvm::reverse(segments)) {
|
|
switch (type_loc_class) {
|
|
case clang::TypeLoc::Elaborated:
|
|
type_str.insert(0, text);
|
|
break;
|
|
case clang::TypeLoc::Qualified:
|
|
if (prev_class == clang::TypeLoc::Pointer) {
|
|
type_str += " " + text;
|
|
} else {
|
|
if (!type_str.empty()) {
|
|
type_str.insert(0, " ");
|
|
}
|
|
type_str.insert(0, text);
|
|
}
|
|
break;
|
|
default:
|
|
type_str += text;
|
|
break;
|
|
}
|
|
prev_class = type_loc_class;
|
|
}
|
|
return type_str;
|
|
}
|
|
|
|
void VarDecl::Run() {
|
|
const auto& decl = GetNodeAsOrDie<clang::VarDecl>(Label);
|
|
if (decl.getTypeSourceInfo() == nullptr) {
|
|
// TODO: Need to understand what's happening in this case. Not sure if we
|
|
// need to address it.
|
|
return;
|
|
}
|
|
|
|
std::string after;
|
|
if (decl.getType().isConstQualified()) {
|
|
after = "let ";
|
|
} else if (!llvm::isa<clang::ParmVarDecl>(&decl)) {
|
|
// Start the replacement with "var" unless it's a parameter.
|
|
after = "var ";
|
|
}
|
|
// Add "identifier: type" to the replacement.
|
|
after += decl.getNameAsString() + ": " + GetTypeStr(decl);
|
|
|
|
// This decides the range to replace. Normally the entire decl is replaced,
|
|
// but for code like `int i, j` we need to detect the comma between the
|
|
// declared names. That case currently results in `var i: int, var j: int`.
|
|
// If there's a comma, this range will be non-empty.
|
|
auto type_loc = decl.getTypeSourceInfo()->getTypeLoc();
|
|
clang::SourceLocation after_type_loc = clang::Lexer::getLocForEndOfToken(
|
|
type_loc.getEndLoc(), 0, GetSourceManager(), GetLangOpts());
|
|
llvm::StringRef comma_source_text = GetSourceText(
|
|
clang::CharSourceRange::getCharRange(after_type_loc, decl.getLocation()));
|
|
clang::SourceLocation replace_start = !comma_source_text.trim().empty()
|
|
? decl.getLocation()
|
|
: decl.getBeginLoc();
|
|
|
|
// Figure out where the replacement ends and initialization begins. For
|
|
// example, `int i` the end is the identifier, `int i[4]` the end is the `[4]`
|
|
// type qualifier.
|
|
clang::SourceLocation identifier_end = clang::Lexer::getLocForEndOfToken(
|
|
decl.getLocation(), 0, GetSourceManager(), GetLangOpts());
|
|
clang::SourceLocation replace_end = std::max(identifier_end, after_type_loc);
|
|
|
|
AddReplacement(
|
|
clang::CharSourceRange::getCharRange(replace_start, replace_end), after);
|
|
}
|
|
|
|
void VarDeclFactory::AddMatcher(cam::MatchFinder* finder,
|
|
cam::MatchFinder::MatchCallback* callback) {
|
|
finder->addMatcher(cam::varDecl(cam::unless(cam::hasParent(cam::declStmt(
|
|
cam::hasParent(cam::cxxForRangeStmt())))))
|
|
.bind(Label),
|
|
callback);
|
|
}
|
|
|
|
} // namespace Carbon
|