mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 20:30:14 +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>;
^
```
126 lines
4.4 KiB
C++
126 lines
4.4 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
|
|
|
|
// An utility for converting between fuzzer protos and Carbon sources.
|
|
//
|
|
// For example, to convert a crashing input in text proto to carbon source:
|
|
// `fuzzverter --mode=proto_to_carbon --input file.textproto`
|
|
//
|
|
// To generate a new text proto from carbon source for seeding the corpus:
|
|
// `fuzzverter --mode=carbon_to_proto --input file.carbon`
|
|
|
|
#include <google/protobuf/text_format.h>
|
|
|
|
#include <cstdlib>
|
|
#include <fstream>
|
|
#include <ios>
|
|
#include <sstream>
|
|
|
|
#include "common/error.h"
|
|
#include "common/fuzzing/carbon.pb.h"
|
|
#include "explorer/common/error_builders.h"
|
|
#include "explorer/fuzzing/ast_to_proto.h"
|
|
#include "explorer/fuzzing/fuzzer_util.h"
|
|
#include "explorer/syntax/parse.h"
|
|
#include "llvm/Support/CommandLine.h"
|
|
#include "llvm/Support/InitLLVM.h"
|
|
|
|
namespace Carbon {
|
|
|
|
namespace cl = llvm::cl;
|
|
|
|
// Reads a file and returns its contents as a string.
|
|
static auto ReadFile(std::string_view file_name) -> ErrorOr<std::string> {
|
|
std::ifstream file(file_name, std::ios::in);
|
|
if (!file.is_open()) {
|
|
return ErrorBuilder() << "Could not open " << file_name << " for reading";
|
|
}
|
|
std::stringstream ss;
|
|
ss << file.rdbuf();
|
|
return ss.str();
|
|
}
|
|
|
|
// Writes string `s` to `file_name`.
|
|
static auto WriteFile(std::string_view s, std::string_view file_name)
|
|
-> ErrorOr<Success> {
|
|
std::ofstream file(file_name, std::ios::out);
|
|
if (!file.is_open()) {
|
|
return ErrorBuilder() << "Could not open " << file_name << " for writing";
|
|
}
|
|
file << s;
|
|
return Success();
|
|
}
|
|
|
|
// Converts text proto to Carbon source.
|
|
static auto TextProtoToCarbon(std::string_view input_file_name,
|
|
std::string_view output_file_name)
|
|
-> ErrorOr<Success> {
|
|
CARBON_ASSIGN_OR_RETURN(const std::string input_contents,
|
|
ReadFile(input_file_name));
|
|
CARBON_ASSIGN_OR_RETURN(const Fuzzing::Carbon carbon_proto,
|
|
ParseCarbonTextProto(input_contents));
|
|
const std::string carbon_source =
|
|
ProtoToCarbonWithMain(carbon_proto.compilation_unit());
|
|
return WriteFile(carbon_source, output_file_name);
|
|
}
|
|
|
|
// Converts Carbon source to text proto.
|
|
static auto CarbonToTextProto(std::string_view input_file_name,
|
|
std::string_view output_file_name)
|
|
-> ErrorOr<Success> {
|
|
Carbon::Arena arena;
|
|
const ErrorOr<AST> ast = Carbon::Parse(&arena, input_file_name,
|
|
/*parser_debug=*/false);
|
|
if (!ast.ok()) {
|
|
return ErrorBuilder() << "Parsing failed: " << ast.error().message();
|
|
}
|
|
Fuzzing::Carbon carbon_proto;
|
|
*carbon_proto.mutable_compilation_unit() = AstToProto(*ast);
|
|
|
|
std::string proto_string;
|
|
google::protobuf::TextFormat::Printer p;
|
|
if (!p.PrintToString(carbon_proto, &proto_string)) {
|
|
return Error("Failed to convert to text proto");
|
|
}
|
|
return WriteFile(proto_string, output_file_name);
|
|
}
|
|
|
|
// Command line options for defining input/output format.
|
|
enum class ConversionMode { TextProtoToCarbon, CarbonToTextProto };
|
|
|
|
auto Main(int argc, char* argv[]) -> ErrorOr<Success> {
|
|
llvm::InitLLVM init_llvm(argc, argv);
|
|
|
|
cl::opt<ConversionMode> mode(
|
|
"mode", cl::desc("Conversion mode"),
|
|
cl::values(
|
|
clEnumValN(ConversionMode::TextProtoToCarbon, "proto_to_carbon",
|
|
"Convert text proto to Carbon source"),
|
|
clEnumValN(ConversionMode::CarbonToTextProto, "carbon_to_proto",
|
|
"Convert Carbon source to text proto")),
|
|
cl::Required);
|
|
cl::opt<std::string> input_file_name("input", cl::desc("<input file>"),
|
|
cl::init("/dev/stdin"));
|
|
cl::opt<std::string> output_file_name("output", cl::desc("<output file>"),
|
|
cl::init("/dev/stdout"));
|
|
cl::ParseCommandLineOptions(argc, argv);
|
|
|
|
switch (mode) {
|
|
case ConversionMode::TextProtoToCarbon:
|
|
return TextProtoToCarbon(input_file_name, output_file_name);
|
|
case ConversionMode::CarbonToTextProto:
|
|
return CarbonToTextProto(input_file_name, output_file_name);
|
|
}
|
|
}
|
|
|
|
} // namespace Carbon
|
|
|
|
auto main(int argc, char* argv[]) -> int {
|
|
if (const auto result = Carbon::Main(argc, argv); !result.ok()) {
|
|
llvm::errs() << result.error().message() << "\n";
|
|
return EXIT_FAILURE;
|
|
}
|
|
return EXIT_SUCCESS;
|
|
}
|