Skip to content

Commit

Permalink
Please the linter with a string view.
Browse files Browse the repository at this point in the history
  • Loading branch information
fruffy committed May 2, 2024
1 parent 89adb4d commit 0478d93
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 32 deletions.
16 changes: 8 additions & 8 deletions backends/p4tools/common/compiler/compiler_target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@

namespace P4Tools {

ICompileContext *CompilerTarget::makeContext(const std::string &toolName) {
ICompileContext *CompilerTarget::makeContext(std::string_view toolName) {
return get(toolName).makeContextImpl();
}

std::vector<const char *> *CompilerTarget::initCompiler(const std::string &toolName, int argc,
std::vector<const char *> *CompilerTarget::initCompiler(std::string_view toolName, int argc,
char **argv) {
return get(toolName).initCompilerImpl(argc, argv);
}

CompilerResultOrError CompilerTarget::runCompiler(const std::string &toolName) {
CompilerResultOrError CompilerTarget::runCompiler(std::string_view toolName) {
const auto *program = P4Tools::CompilerTarget::runParser();
if (program == nullptr) {
return std::nullopt;
Expand All @@ -36,7 +36,7 @@ CompilerResultOrError CompilerTarget::runCompiler(const std::string &toolName) {
return runCompiler(toolName, program);
}

CompilerResultOrError CompilerTarget::runCompiler(const std::string &toolName,
CompilerResultOrError CompilerTarget::runCompiler(std::string_view toolName,
const std::string &source) {
const auto *program = P4::parseP4String(source, P4CContext::get().options().langVersion);
if (program == nullptr) {
Expand All @@ -46,7 +46,7 @@ CompilerResultOrError CompilerTarget::runCompiler(const std::string &toolName,
return runCompiler(toolName, program);
}

CompilerResultOrError CompilerTarget::runCompiler(const std::string &toolName,
CompilerResultOrError CompilerTarget::runCompiler(std::string_view toolName,
const IR::P4Program *program) {
return get(toolName).runCompilerImpl(program);
}
Expand Down Expand Up @@ -118,12 +118,12 @@ const IR::P4Program *CompilerTarget::runMidEnd(const IR::P4Program *program) con
return program->apply(midEnd);
}

CompilerTarget::CompilerTarget(const std::string &toolName, const std::string &deviceName,
CompilerTarget::CompilerTarget(std::string_view toolName, const std::string &deviceName,
const std::string &archName)
: Target(toolName, deviceName, archName) {}

const CompilerTarget &CompilerTarget::get(const std::string &toolName) {
return Target::get<CompilerTarget>(toolName);
const CompilerTarget &CompilerTarget::get(std::string_view toolName) {
return Target::get<CompilerTarget>(toolName.data());
}

} // namespace P4Tools
15 changes: 7 additions & 8 deletions backends/p4tools/common/compiler/compiler_target.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,30 @@ namespace P4Tools {
class CompilerTarget : public Target {
public:
/// @returns a new compilation context for the compiler.
static ICompileContext *makeContext(const std::string &toolName);
static ICompileContext *makeContext(std::string_view toolName);

/// Initializes the P4 compiler with the given compiler-specific command-line arguments.
///
/// @returns any unprocessed arguments, or nullptr if there was an error.
static std::vector<const char *> *initCompiler(const std::string &toolName, int argc,
static std::vector<const char *> *initCompiler(std::string_view toolName, int argc,
char **argv);

/// Runs the P4 compiler to produce an IR and various other kinds of information on the input
/// program.
///
/// @returns std::nullopt if an error occurs during compilation.
static CompilerResultOrError runCompiler(const std::string &toolName);
static CompilerResultOrError runCompiler(std::string_view toolName);

/// Runs the P4 compiler to produce an IR and other information for the given source code.
///
/// @returns std::nullopt if an error occurs during compilation.
static CompilerResultOrError runCompiler(const std::string &toolName,
const std::string &source);
static CompilerResultOrError runCompiler(std::string_view toolName, const std::string &source);

private:
/// Runs the front and mid ends on the given parsed program.
///
/// @returns std::nullopt if an error occurs during compilation.
static CompilerResultOrError runCompiler(const std::string &toolName, const IR::P4Program *);
static CompilerResultOrError runCompiler(std::string_view toolName, const IR::P4Program *);

protected:
/// @see @makeContext.
Expand Down Expand Up @@ -77,12 +76,12 @@ class CompilerTarget : public Target {
/// @returns nullptr if an error occurs during compilation.
const IR::P4Program *runMidEnd(const IR::P4Program *program) const;

explicit CompilerTarget(const std::string &toolName, const std::string &deviceName,
explicit CompilerTarget(std::string_view toolName, const std::string &deviceName,
const std::string &archName);

private:
/// @returns the singleton instance for the current target.
static const CompilerTarget &get(const std::string &toolName);
static const CompilerTarget &get(std::string_view toolName);
};

} // namespace P4Tools
Expand Down
6 changes: 3 additions & 3 deletions backends/p4tools/common/core/target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ const IR::Expression *Target::createTargetUninitialized(const IR::Type *type,
return IR::getDefaultValue(type);
}

Target::Target(const std::string &toolName, const std::string &deviceName,
Target::Target(std::string_view toolName, const std::string &deviceName,
const std::string &archName)
: toolName(toolName), spec(deviceName, archName) {
// Register this instance.
BUG_CHECK(!registry[spec].count(toolName), "Already registered %1%/%2% instance for %3%",
BUG_CHECK(!registry[spec].count(toolName.data()), "Already registered %1%/%2% instance for %3%",
deviceName, archName, toolName);
registry[spec][toolName] = this;
registry[spec][toolName.data()] = this;

// Register default device and architecture, if needed.
if (defaultDeviceByArch.count(spec.archName) == 0U) {
Expand Down
11 changes: 6 additions & 5 deletions backends/p4tools/common/core/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,24 @@ class Target {
protected:
/// Creates and registers a new Target instance for the given @toolName, @deviceName, and
/// @archName.
Target(const std::string &toolName, const std::string &deviceName, const std::string &archName);
Target(std::string_view toolName, const std::string &deviceName, const std::string &archName);

/// @returns the target instance for the given tool and active target, as selected by @init.
//
// Implemented here because of limitations of templates.
template <class TargetImpl>
static const TargetImpl &get(const std::string &toolName) {
static const TargetImpl &get(std::string_view toolName) {
if (curTarget == std::nullopt) {
FATAL_ERROR(
"Target not initialized. Please provide a target using the --target option.");
}

const auto &instances = registry.at(*curTarget);
BUG_CHECK(instances.count(toolName), "Architecture %1% on device %2% not supported for %3%",
curTarget->archName, curTarget->deviceName, toolName);
BUG_CHECK(instances.count(toolName.data()),
"Architecture %1% on device %2% not supported for %3%", curTarget->archName,
curTarget->deviceName, toolName);

const auto *instance = instances.at(toolName);
const auto *instance = instances.at(toolName.data());
const auto *casted = dynamic_cast<const TargetImpl *>(instance);
BUG_CHECK(casted, "%1%/%2% implementation for %3% has wrong type", curTarget->deviceName,
curTarget->archName, toolName);
Expand Down
2 changes: 1 addition & 1 deletion backends/p4tools/common/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ struct InheritedCompilerOptionSpec {
std::optional<std::function<bool(const char *)>> handler;
};

AbstractP4cToolOptions::AbstractP4cToolOptions(const std::string &toolName, cstring message)
AbstractP4cToolOptions::AbstractP4cToolOptions(std::string_view toolName, cstring message)
: Options(message), _toolName(toolName) {
// Register some common options.
registerOption(
Expand Down
2 changes: 1 addition & 1 deletion backends/p4tools/common/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class AbstractP4cToolOptions : protected Util::Options {
/// Converts a vector of command-line arguments into the traditional (argc, argv) format.
static std::tuple<int, char **> convertArgs(const std::vector<const char *> &args);

explicit AbstractP4cToolOptions(const std::string &toolName, cstring message);
explicit AbstractP4cToolOptions(std::string_view toolName, cstring message);
};

} // namespace P4Tools
Expand Down
2 changes: 1 addition & 1 deletion backends/p4tools/common/p4ctool.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class AbstractP4cTool {
/// @param args
/// Contains the path to the executable, followed by the command-line arguments for this
/// tool.
int main(const std::string &toolName, const std::vector<const char *> &args) {
int main(std::string_view toolName, const std::vector<const char *> &args) {
// Register supported compiler targets.
registerTarget();

Expand Down
10 changes: 5 additions & 5 deletions backends/p4tools/modules/testgen/toolname.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_TOOLNAME_H
#define BACKENDS_P4TOOLS_MODULES_TESTGEN_TOOLNAME_H
#ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_TOOLNAME_H_
#define BACKENDS_P4TOOLS_MODULES_TESTGEN_TOOLNAME_H_

#include <string>
#include <string_view>

namespace P4Tools::P4Testgen {

static const std::string TOOL_NAME = "testgen";
static inline constexpr std::string_view TOOL_NAME = "testgen";

} // namespace P4Tools::P4Testgen

#endif /* BACKENDS_P4TOOLS_MODULES_TESTGEN_TOOLNAME_H */
#endif /* BACKENDS_P4TOOLS_MODULES_TESTGEN_TOOLNAME_H_ */

0 comments on commit 0478d93

Please sign in to comment.