Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some problems and suggestions found by clang-tidy #4237

Merged
merged 14 commits into from
Nov 13, 2023
1 change: 0 additions & 1 deletion frontends/common/constantFolding.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ class DoConstantFolding : public Transform {
// we substituting constants there.
bool assignmentTarget;

protected:
/// @returns a constant equivalent to @p expr or `nullptr`
const IR::Expression *getConstant(const IR::Expression *expr) const;

Expand Down
4 changes: 2 additions & 2 deletions frontends/p4/coreLibrary.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,19 @@ class P4Exception_Model : public ::Model::Elem {
// To be kept in sync with core.p4
class P4CoreLibrary : public ::Model::Model {
protected:
// NOLINTBEGIN(bugprone-throw-keyword-missing)
P4CoreLibrary()
: noAction("NoAction"),
exactMatch("exact"),
ternaryMatch("ternary"),
lpmMatch("lpm"),
packetIn(PacketIn()),
packetOut(PacketOut()),
noError(StandardExceptions::NoError),
packetTooShort(StandardExceptions::PacketTooShort),
noMatch(StandardExceptions::NoMatch),
stackOutOfBounds(StandardExceptions::StackOutOfBounds),
overwritingHeader(StandardExceptions::OverwritingHeader),
headerTooShort(StandardExceptions::HeaderTooShort) {}
// NOLINTEND(bugprone-throw-keyword-missing)

public:
static P4CoreLibrary &instance() {
Expand Down
12 changes: 5 additions & 7 deletions frontends/p4/fromv1.0/converters.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ class ComputeCallGraph : public Inspector {
auto name = primitive->name;
const IR::GlobalRef *glob = nullptr;
const IR::Declaration_Instance *extrn = nullptr;
if (primitive->operands.size() >= 1) glob = primitive->operands[0]->to<IR::GlobalRef>();
if (!primitive->operands.empty()) glob = primitive->operands[0]->to<IR::GlobalRef>();
if (glob) extrn = glob->obj->to<IR::Declaration_Instance>();

if (extrn) {
Expand Down Expand Up @@ -798,7 +798,7 @@ class InsertCompilerGeneratedStartState : public Transform {

// rename original start state
const IR::Node *postorder(IR::ParserState *state) override {
if (!structure->parserEntryPoints.size()) return state;
if (structure->parserEntryPoints.empty()) return state;
if (state->name == IR::ParserState::start) {
state->name = newStartState;
}
Expand All @@ -807,7 +807,7 @@ class InsertCompilerGeneratedStartState : public Transform {

// Rename any path refering to original start state
const IR::Node *postorder(IR::Path *path) override {
if (!structure->parserEntryPoints.size()) return path;
if (structure->parserEntryPoints.empty()) return path;
// At this point any identifier called start should have been renamed
// to unique name (e.g. start_1) => we can safely assume that any
// "start" refers to the parser state
Expand All @@ -825,7 +825,7 @@ class InsertCompilerGeneratedStartState : public Transform {
}

const IR::Node *postorder(IR::P4Parser *parser) override {
if (!structure->parserEntryPoints.size()) return parser;
if (structure->parserEntryPoints.empty()) return parser;
IR::IndexedVector<IR::SerEnumMember> members;
// transition to original start state
members.push_back(new IR::SerEnumMember("START", new IR::Constant(0)));
Expand Down Expand Up @@ -1023,9 +1023,7 @@ class FindRecirculated : public Inspector {
}

void postorder(const IR::Primitive *primitive) override {
if (primitive->name == "recirculate") {
add(primitive, 0);
} else if (primitive->name == "resubmit") {
if (primitive->name == "recirculate" || primitive->name == "resubmit") {
add(primitive, 0);
} else if (primitive->name.startsWith("clone") && primitive->operands.size() == 2) {
add(primitive, 1);
Expand Down
1 change: 0 additions & 1 deletion frontends/p4/fromv1.0/programStructure.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ class ProgramStructure {
const IR::Parameter *parserPacketIn = nullptr;
const IR::Parameter *parserHeadersOut = nullptr;

public:
// output is constructed here
IR::Vector<IR::Node> *declarations;

Expand Down
5 changes: 2 additions & 3 deletions frontends/p4/fromv1.0/v1model.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ struct CounterOrMeter_Model : public ::Model::Extern_Model {
: Extern_Model(name),
sizeParam("size"),
typeParam("type"),
size_type(IR::Type_Bits::get(32)),
counterType() {}
size_type(IR::Type_Bits::get(32)) {}
::Model::Elem sizeParam;
::Model::Elem typeParam;
const IR::Type *size_type;
Expand Down Expand Up @@ -218,7 +217,7 @@ struct Cloner_Model : public ::Model::Extern_Model {
Cloner_Model()
: Extern_Model("clone"),
clone3("clone_preserving_field_list"),
cloneType(),

sessionType(IR::Type_Bits::get(32)) {}
::Model::Elem clone3;
CloneType_Model cloneType;
Expand Down
3 changes: 2 additions & 1 deletion ir/dbprint.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ int dbsetflags(std::ostream &out, int val, int mask = ~0U);
inline int getprec(std::ostream &out) { return dbgetflags(out) & DBPrint::Precedence; }
class setflags_helper {
int val, mask;
setflags_helper() = delete;

protected:
setflags_helper(int v, int m) : val(v), mask(m) { assert((val & ~mask) == 0); }

public:
setflags_helper() = delete;

void set(std::ostream &out) const { dbsetflags(out, val, mask); }
};
struct setprec : public setflags_helper {
Expand Down
5 changes: 3 additions & 2 deletions ir/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,18 @@ inline bool equiv(const Node *a, const Node *b) { return a == b || (a && b && a-
inline bool equiv(const INode *a, const INode *b) {
return a == b || (a && b && a->getNode()->equiv(*b->getNode()));
}

// NOLINTBEGIN(bugprone-macro-parentheses)
/* common things that ALL Node subclasses must define */
#define IRNODE_SUBCLASS(T) \
public: \
T *clone() const override { return new T(*this); } \
IRNODE_COMMON_SUBCLASS(T)

#define IRNODE_ABSTRACT_SUBCLASS(T) \
public: \
T *clone() const override = 0; \
IRNODE_COMMON_SUBCLASS(T)

// NOLINTEND(bugprone-macro-parentheses)
#define IRNODE_COMMON_SUBCLASS(T) \
public: \
using Node::operator==; \
Expand Down
5 changes: 3 additions & 2 deletions ir/visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,14 @@ class Visitor {
Visitor &v;
uint64_t start;
explicit profile_t(Visitor &);
friend class Visitor;

public:
profile_t() = delete;
profile_t(const profile_t &) = delete;
profile_t &operator=(const profile_t &) = delete;
profile_t &operator=(profile_t &&) = delete;
friend class Visitor;

public:
~profile_t();
profile_t(profile_t &&);
};
Expand Down
4 changes: 2 additions & 2 deletions lib/compile_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class ICompileContext {
/// Compilation contexts can be nested to allow composing programs without
/// intermingling their stack.
struct CompileContextStack final {
CompileContextStack() = delete;

/// @return the current compilation context (i.e., the top of the
/// compilation context stack), cast to the requested type. If the current
/// compilation context is of the wrong type, or the stack is empty, an
Expand Down Expand Up @@ -64,8 +66,6 @@ struct CompileContextStack final {
static void push(ICompileContext *context);
static void pop();
static StackType &getStack();

CompileContextStack() = delete;
};

/// A RAII helper which pushes a compilation context onto the stack when it's
Expand Down
2 changes: 1 addition & 1 deletion lib/error_catalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class ErrorCatalog {
}

/// retrieve the name for errorCode
const cstring getName(int errorCode) {
cstring getName(int errorCode) {
if (errorCatalog.count(errorCode)) return errorCatalog.at(errorCode);
return "--unknown--";
}
Expand Down
6 changes: 3 additions & 3 deletions lib/error_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ struct ErrorMessage {
enum class MessageType : std::size_t { None, Error, Warning, Info };

MessageType type = MessageType::None;
std::string prefix = ""; /// Typically error/warning type from catalog
std::string message = ""; /// Particular formatted message
std::string prefix; /// Typically error/warning type from catalog
std::string message; /// Particular formatted message
std::vector<Util::SourceInfo> locations = {}; /// Relevant source locations for this error
std::string suffix = ""; /// Used by errorWithSuffix
std::string suffix; /// Used by errorWithSuffix

ErrorMessage() {}
// Invoked from backwards compatible error_helper
Expand Down
40 changes: 22 additions & 18 deletions lib/hashvec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,28 +216,32 @@ hash_vector_base::hash_vector_base(hash_vector_base &&a)
}

hash_vector_base &hash_vector_base::operator=(const hash_vector_base &a) {
freehash();
info = a.info;
hashsize = a.hashsize;
inuse = a.inuse;
collisions = a.collisions;
log_hashsize = a.log_hashsize;
erased = a.erased;
allochash();
memcpy(hash.s1, a.hash.s1, hashsize * info->hashelsize);
if (this != &a) {
freehash();
info = a.info;
hashsize = a.hashsize;
inuse = a.inuse;
collisions = a.collisions;
log_hashsize = a.log_hashsize;
erased = a.erased;
allochash();
memcpy(hash.s1, a.hash.s1, hashsize * info->hashelsize);
}
return *this;
}

hash_vector_base &hash_vector_base::operator=(hash_vector_base &&a) {
freehash();
info = a.info;
hashsize = a.hashsize;
inuse = a.inuse;
collisions = a.collisions;
log_hashsize = a.log_hashsize;
erased = a.erased;
hash.s1 = a.hash.s1;
a.hash.s1 = nullptr;
if (this != &a) {
freehash();
info = a.info;
hashsize = a.hashsize;
inuse = a.inuse;
collisions = a.collisions;
log_hashsize = a.log_hashsize;
erased = a.erased;
hash.s1 = a.hash.s1;
a.hash.s1 = nullptr;
}
return *this;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/indent.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ inline std::ostream &unindent(std::ostream &out) {

class TempIndent {
// an indent that can be added to any stream and unrolls when the object is destroyed
std::vector<std::ostream *> streams; // streams that have been indented
TempIndent(const TempIndent &) = delete; // not copyable
std::vector<std::ostream *> streams; // streams that have been indented

public:
TempIndent(const TempIndent &) = delete; // not copyable
TempIndent() = default;
friend std::ostream &operator<<(std::ostream &out, TempIndent &ti) {
ti.streams.push_back(&out);
Expand Down
2 changes: 2 additions & 0 deletions lib/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ void increaseVerbosity();
#define MAX_LOGGING_LEVEL 10
#endif

// NOLINTBEGIN(bugprone-macro-parentheses)
#define LOGGING(N) \
((N) <= MAX_LOGGING_LEVEL && ::Log::fileLogLevelIsAtLeast(__FILE__, N) && \
::Log::enableLogging())
Expand Down Expand Up @@ -144,6 +145,7 @@ void increaseVerbosity();
#define P4C_ERROR(X) (std::clog << "ERROR: " << X << std::endl)
#define P4C_WARNING(X) (::Log::verbose() ? std::clog << "WARNING: " << X << std::endl : std::clog)
#define ERRWARN(C, X) ((C) ? P4C_ERROR(X) : P4C_WARNING(X))
// NOLINTEND(bugprone-macro-parentheses)

static inline std::ostream &operator<<(std::ostream &out,
std::function<std::ostream &(std::ostream &)> fn) {
Expand Down
1 change: 0 additions & 1 deletion lib/ordered_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ class ordered_map {
public:
typedef typename map_type::size_type size_type;

public:
ordered_map() {}
ordered_map(const ordered_map &a) : data(a.data) { init_data_map(); }
template <typename InputIt>
Expand Down
12 changes: 6 additions & 6 deletions lib/safe_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ limitations under the License.

/// An enhanced version of std::vector that performs bounds checking for
/// operator[].
template <class T, class _Alloc = std::allocator<T>>
class safe_vector : public std::vector<T, _Alloc> {
template <class T, class Alloc = std::allocator<T>>
class safe_vector : public std::vector<T, Alloc> {
public:
using std::vector<T, _Alloc>::vector;
typedef typename std::vector<T, _Alloc>::reference reference;
typedef typename std::vector<T, _Alloc>::const_reference const_reference;
typedef typename std::vector<T, _Alloc>::size_type size_type;
using std::vector<T, Alloc>::vector;
typedef typename std::vector<T, Alloc>::reference reference;
typedef typename std::vector<T, Alloc>::const_reference const_reference;
typedef typename std::vector<T, Alloc>::size_type size_type;
typedef typename std::vector<T>::const_iterator const_iterator;
reference operator[](size_type n) { return this->at(n); }
const_reference operator[](size_type n) const { return this->at(n); }
Expand Down
2 changes: 1 addition & 1 deletion lib/sourceCodeBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class SourceCodeBuilder {
newline();
}
void append(const std::string &str) {
if (str.size() == 0) return;
if (str.empty()) return;
endsInSpace = ::isspace(str.at(str.size() - 1));
buffer << str;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/source_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class SourceInfo final {
this->srcBrief = srcBrief;
}
/// Creates an "invalid" SourceInfo
SourceInfo() : sources(nullptr), start(SourcePosition()), end(SourcePosition()) {}
SourceInfo() : sources(nullptr) {}

/// Creates a SourceInfo for a 'point' in the source, or invalid
SourceInfo(const InputSources *sources, SourcePosition point)
Expand All @@ -153,7 +153,7 @@ class SourceInfo final {
/**
A SourceInfo that spans both this and rhs.
However, if this or rhs is invalid, it is not taken into account */
const SourceInfo operator+(const SourceInfo &rhs) const {
SourceInfo operator+(const SourceInfo &rhs) const {
if (!this->isValid()) return rhs;
if (!rhs.isValid()) return *this;
SourcePosition s = start.min(rhs.start);
Expand Down
3 changes: 3 additions & 0 deletions lib/stringref.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@ struct StringRef {
len = 0;
}
StringRef(const StringRef &a) : p(a.p), len(a.len) {}
// avoid clang-tidy complaining about assignment that is actually safe
// NOLINTBEGIN(bugprone-unhandled-self-assignment)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    if(this == &a)
      return *this;

instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is safe to be performed, I'm not sure if the if is worth having in such cases. If the if is there, one would have to wonder why it is needed.

StringRef &operator=(const StringRef &a) {
p = a.p;
len = a.len;
return *this;
}
// NOLINTEND(bugprone-unhandled-self-assignment)
explicit operator bool() const { return p != 0; }

bool operator==(const StringRef &a) const {
Expand Down
Loading