Skip to content

Commit

Permalink
Apply clang-tidy to the project
Browse files Browse the repository at this point in the history
Changed the way clang-tidy is applied; now it's an addon to the compilation.
  • Loading branch information
lucteo committed Jul 7, 2019
1 parent 6b8db4b commit bbcc915
Show file tree
Hide file tree
Showing 33 changed files with 104 additions and 136 deletions.
34 changes: 18 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ option(BOOTSTRAP_SPARROW "Use system-wide SparrowCompiler to compile Sparrow fil
message(STATUS "BOOTSTRAP_SPARROW: ${BOOTSTRAP_SPARROW}")
option(SPARROW_PROFILING "Enable Tracy integration into Sparrow compiler" OFF)
message(STATUS "SPARROW_PROFILING: ${SPARROW_PROFILING}")
option(ENABLE_TIDY "Enable clang-tidy checks when building" OFF)
message(STATUS "ENABLE_TIDY: ${ENABLE_TIDY}")

# Where to output the results of the compilation
set(OutDir ${CMAKE_CURRENT_SOURCE_DIR}/build/bin)
Expand Down Expand Up @@ -121,39 +123,39 @@ endif()

# Add target for clang-tidy
set(TIDY_CHECKS
#-*, # Disable for non-debug builds
-*, # Disable for non-debug builds

modernize-*,
bugprone-*,
cppcoreguidelines-*,
-cppcoreguidelines-owning-memory, # not met, requires gsl
-cppcoreguidelines-pro-bounds-array-to-pointer-decay, # not met, requires gsl
-cppcoreguidelines-pro-bounds-constant-array-index, # not met, requires gsl
-cppcoreguidelines-pro-bounds-pointer-arithmetic, # not met, requires gsl
-cppcoreguidelines-pro-type-union-access, # not met (we use some unions)
-cppcoreguidelines-pro-type-vararg, # not met (we are using printf)
)
string (REPLACE ";" " " TIDY_CHECKS "${TIDY_CHECKS}")

find_program(CLANG_TIDY_SCRIPT "run-clang-tidy.py" HINTS ${LLVM_TOOLS_BINARY_DIR}/../share/clang)
if(CLANG_TIDY_SCRIPT)
if(ENABLE_TIDY)
# Search for clang-tidy and clang-apply-replacements
find_program(CLANG_TIDY "clang-tidy" HINTS ${LLVM_TOOLS_BINARY_DIR})
find_program(CLANG_APPLY_REPLACEMENTS "clang-apply-replacements" HINTS ${LLVM_TOOLS_BINARY_DIR})

message(STATUS "Tidy checks: ${TIDY_CHECKS}")

set(TIDY_HEADER_FILTER ${CMAKE_SOURCE_DIR}/*)

# Set extra arguments
set(TIDY_FIX 0)
set(TIDY_EXTRA_ARGS -quiet -format -style file)
set(TIDY_FIX 1)
set(TIDY_EXTRA_ARGS -quiet;-format-style=file)
if (TIDY_FIX)
set(TIDY_EXTRA_ARGS ${TIDY_EXTRA_ARGS} -j 1 -fix)
set(TIDY_EXTRA_ARGS ${TIDY_EXTRA_ARGS};-fix)
endif()

# Actually add the target
add_custom_target(
clang-tidy
COMMAND
${CLANG_TIDY_SCRIPT} -header-filter='\.\.\/src\/.*\.hpp' -checks='${TIDY_CHECKS}'
-clang-tidy-binary ${CLANG_TIDY}
-clang-apply-replacements-binary ${CLANG_APPLY_REPLACEMENTS}
${TIDY_EXTRA_ARGS}
)
set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY};-checks=\"${TIDY_CHECKS}\";-header-filter='${TIDY_HEADER_FILTER}';${TIDY_EXTRA_ARGS}"
CACHE STRING "" FORCE)
else()
set(CMAKE_CXX_CLANG_TIDY "")
endif()

# Ensure generation of compile_commands.json; needed for clang-tidy
Expand Down
6 changes: 3 additions & 3 deletions src/Feather/Utils/cppif/FeatherNodes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ struct FunctionDecl : DeclNode {
void setBody(NodeHandle body);

//! @copydoc Nest::NodeHandle::type()
FunctionType type() const { return FunctionType(DeclNode::type()); }
FunctionType type() const { return {DeclNode::type()}; }

private:
static void setContextForChildrenImpl(ThisNodeType node);
Expand Down Expand Up @@ -452,7 +452,7 @@ struct StructDecl : DeclNode {
NodeRange fields() const;

//! @copydoc Nest::NodeHandle::type()
DataType type() const { return DataType(DeclNode::type()); }
DataType type() const { return {DeclNode::type()}; }

private:
static void setContextForChildrenImpl(ThisNodeType node);
Expand Down Expand Up @@ -489,7 +489,7 @@ struct VarDecl : DeclNode {
NodeHandle typeNode() const;

//! @copydoc Nest::NodeHandle::type()
TypeWithStorage type() const { return TypeWithStorage(DeclNode::type()); }
TypeWithStorage type() const { return {DeclNode::type()}; }

private:
static void setContextForChildrenImpl(ThisNodeType node);
Expand Down
22 changes: 11 additions & 11 deletions src/Feather/Utils/cppif/FeatherTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct DataType : TypeWithStorage {

//! @copydoc Type::changeMode
DataType changeMode(Nest::EvalMode mode, Nest::Location loc = Nest::Location{}) const {
return DataType(Type::changeMode(mode, loc));
return {Type::changeMode(mode, loc)};
}
};

Expand Down Expand Up @@ -82,14 +82,14 @@ struct ConstType : TypeWithStorage {
static ConstType get(TypeWithStorage base, Nest::Location loc = {});

//! Returns the base type of this type
TypeWithStorage base() const { return TypeWithStorage(type_->subTypes[0]); }
TypeWithStorage base() const { return {type_->subTypes[0]}; }

//! Transform this type into a corresponding DataType with the same number of references.
DataType toRef() const;

//! @copydoc Type::changeMode
ConstType changeMode(Nest::EvalMode mode, Nest::Location loc = Nest::Location{}) const {
return ConstType(Type::changeMode(mode, loc));
return {Type::changeMode(mode, loc)};
}
};

Expand Down Expand Up @@ -117,14 +117,14 @@ struct MutableType : TypeWithStorage {
static MutableType get(TypeWithStorage base, Nest::Location loc = {});

//! Returns the base type of this type
TypeWithStorage base() const { return TypeWithStorage(type_->subTypes[0]); }
TypeWithStorage base() const { return {type_->subTypes[0]}; }

//! Transform this type into a corresponding DataType with the same number of references.
DataType toRef() const;

//! @copydoc Type::changeMode
MutableType changeMode(Nest::EvalMode mode, Nest::Location loc = Nest::Location{}) const {
return MutableType(Type::changeMode(mode, loc));
return {Type::changeMode(mode, loc)};
}
};

Expand Down Expand Up @@ -152,14 +152,14 @@ struct TempType : TypeWithStorage {
static TempType get(TypeWithStorage base, Nest::Location loc = {});

//! Returns the base type of this type
TypeWithStorage base() const { return TypeWithStorage(type_->subTypes[0]); }
TypeWithStorage base() const { return {type_->subTypes[0]}; }

//! Transform this type into a corresponding DataType with the same number of references.
DataType toRef() const;

//! @copydoc Type::changeMode
TempType changeMode(Nest::EvalMode mode, Nest::Location loc = Nest::Location{}) const {
return TempType(Type::changeMode(mode, loc));
return {Type::changeMode(mode, loc)};
}
};

Expand All @@ -185,13 +185,13 @@ struct ArrayType : TypeWithStorage {
static ArrayType get(TypeWithStorage unitType, int count);

//! Returns the unit type of this array type
TypeWithStorage unitType() const { return TypeWithStorage(type_->subTypes[0]); }
TypeWithStorage unitType() const { return {type_->subTypes[0]}; }
//! Returns the number of elements in the array
int count() const { return type_->flags; }

//! @copydoc Type::changeMode
ArrayType changeMode(Nest::EvalMode mode, Nest::Location loc = Nest::Location{}) const {
return ArrayType(Type::changeMode(mode, loc));
return {Type::changeMode(mode, loc)};
}
};

Expand Down Expand Up @@ -226,11 +226,11 @@ struct FunctionType : TypeWithStorage {
//! Returns the number of parameters that we have
int numParams() const { return type_->numSubtypes - 1; }
//! Access operators for the parameters
TypeWithStorage operator[](int idx) const { return TypeWithStorage(type_->subTypes[idx + 1]); }
TypeWithStorage operator[](int idx) const { return {type_->subTypes[idx + 1]}; }

//! @copydoc Type::changeMode
FunctionType changeMode(Nest::EvalMode mode, Nest::Location loc = Nest::Location{}) const {
return FunctionType(Type::changeMode(mode, loc));
return {Type::changeMode(mode, loc)};
}
};

Expand Down
12 changes: 5 additions & 7 deletions src/Feather/src/Utils/cppif/FeatherNodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,9 +596,7 @@ CtValueExp CtValueExp::create(const Location& loc, TypeWithStorage type, StringR
res.setProperty("valueData", data);
return res;
}
TypeWithStorage CtValueExp::valueType() const {
return TypeWithStorage(getCheckPropertyType("valueType"));
}
TypeWithStorage CtValueExp::valueType() const { return {getCheckPropertyType("valueType")}; }
StringRef CtValueExp::valueData() const { return getCheckPropertyString("valueData"); }
NodeHandle CtValueExp::semanticCheckImpl(CtValueExp node) {
// Check the type
Expand Down Expand Up @@ -701,7 +699,7 @@ VarRefExp VarRefExp::create(const Location& loc, VarDecl varDecl) {
res.setReferredNodes(NodeRange{varDecl});
return res;
}
VarDecl VarRefExp::varDecl() const { return VarDecl(referredNodes()[0]); }
VarDecl VarRefExp::varDecl() const { return {referredNodes()[0]}; }
NodeHandle VarRefExp::semanticCheckImpl(VarRefExp node) {
VarDecl var = node.varDecl();
ASSERT(var);
Expand Down Expand Up @@ -737,7 +735,7 @@ FieldRefExp FieldRefExp::create(const Location& loc, NodeHandle obj, VarDecl fie
return res;
}
NodeHandle FieldRefExp::object() const { return children()[0]; }
VarDecl FieldRefExp::fieldDecl() const { return VarDecl(referredNodes()[0]); }
VarDecl FieldRefExp::fieldDecl() const { return {referredNodes()[0]}; }
NodeHandle FieldRefExp::semanticCheckImpl(FieldRefExp node) {
NodeHandle obj = node.object();
VarDecl field = node.fieldDecl();
Expand Down Expand Up @@ -806,7 +804,7 @@ FunRefExp FunRefExp::create(const Location& loc, FunctionDecl funDecl, NodeHandl
res.setReferredNodes(NodeRange{funDecl});
return res;
}
FunctionDecl FunRefExp::funDecl() const { return FunctionDecl(referredNodes()[0]); }
FunctionDecl FunRefExp::funDecl() const { return {referredNodes()[0]}; }
NodeHandle FunRefExp::resTypeNode() const { return children()[0]; }
NodeHandle FunRefExp::semanticCheckImpl(FunRefExp node) {
NodeHandle resType = node.resTypeNode();
Expand All @@ -833,7 +831,7 @@ FunCallExp FunCallExp::create(const Location& loc, FunctionDecl funDecl, NodeRan
res.setReferredNodes(NodeRange{funDecl});
return res;
}
FunctionDecl FunCallExp::funDecl() const { return FunctionDecl(referredNodes()[0]); }
FunctionDecl FunCallExp::funDecl() const { return {referredNodes()[0]}; }
NodeRange FunCallExp::arguments() const { return children(); }
NodeHandle FunCallExp::semanticCheckImpl(FunCallExp node) {
FunctionDecl fun = node.funDecl();
Expand Down
18 changes: 9 additions & 9 deletions src/Feather/src/Utils/cppif/FeatherTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ DataType::DataType(Nest::TypeRef type)
}

DataType DataType::get(Nest::NodeHandle decl, int numReferences, Nest::EvalMode mode) {
return DataType(Feather_getDataType(decl, numReferences, mode));
return {Feather_getDataType(decl, numReferences, mode)};
}

ConstType::ConstType(Nest::TypeRef type)
Expand All @@ -39,10 +39,10 @@ ConstType ConstType::get(TypeWithStorage base, Nest::Location loc) {
REP_INTERNAL(loc, "Null type given as base to const type");
int baseKind = base.kind();
if (baseKind == typeKindConst)
return ConstType(base);
return {base};
else if (baseKind == typeKindMutable || baseKind == typeKindTemp)
REP_ERROR(loc, "Cannot construct a const type based on %1%") % base;
return ConstType(Feather_getConstType(base));
return {Feather_getConstType(base)};
}

DataType ConstType::toRef() const { return DataType::get(referredNode(), numReferences(), mode()); }
Expand All @@ -58,10 +58,10 @@ MutableType MutableType::get(TypeWithStorage base, Nest::Location loc) {
REP_INTERNAL(loc, "Null type given as base to const type");
int baseKind = base.kind();
if (baseKind == typeKindMutable)
return MutableType(base);
return {base};
else if (baseKind == typeKindConst || baseKind == typeKindTemp)
REP_ERROR(loc, "Cannot construct a mutable type based on %1%") % base;
return MutableType(Feather_getMutableType(base));
return {Feather_getMutableType(base)};
}

DataType MutableType::toRef() const {
Expand All @@ -79,10 +79,10 @@ TempType TempType::get(TypeWithStorage base, Nest::Location loc) {
REP_INTERNAL(loc, "Null type given as base to const type");
int baseKind = base.kind();
if (baseKind == typeKindTemp)
return TempType(base);
return {base};
else if (baseKind == typeKindConst || baseKind == typeKindMutable)
REP_ERROR(loc, "Cannot construct a tmp type based on %1%") % base;
return TempType(Feather_getTempType(base));
return {Feather_getTempType(base)};
}

DataType TempType::toRef() const { return DataType::get(referredNode(), numReferences(), mode()); }
Expand All @@ -94,7 +94,7 @@ ArrayType::ArrayType(Nest::TypeRef type)
}

ArrayType ArrayType::get(TypeWithStorage unitType, int count) {
return ArrayType(Feather_getArrayType(unitType, count));
return {Feather_getArrayType(unitType, count)};
}

FunctionType::FunctionType(Nest::TypeRef type)
Expand All @@ -105,7 +105,7 @@ FunctionType::FunctionType(Nest::TypeRef type)

FunctionType FunctionType::get(
Nest::TypeRef* resultTypeAndParams, int numTypes, Nest::EvalMode mode) {
return FunctionType(Feather_getFunctionType(resultTypeAndParams, numTypes, mode));
return {Feather_getFunctionType(resultTypeAndParams, numTypes, mode)};
}

bool isDataLikeType(Type type) {
Expand Down
5 changes: 2 additions & 3 deletions src/Nest/Utils/cppif/NodeHandle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ struct NodeRangeM;
*/
struct NodeHandle {
//! The actual handle to the node
Nest_Node* handle;
NodeHandle()
: handle(nullptr){};
Nest_Node* handle{nullptr};
NodeHandle(){};
//! Construct a node handle from an actual C-style Nest_Node pointer
NodeHandle(Nest_Node* h)
: handle(h) {}
Expand Down
8 changes: 3 additions & 5 deletions src/Nest/Utils/cppif/StringRef.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@ struct StringRef : Nest_StringRef {

//! The mutable version of StringRef -- we can modify its content
struct StringRefM {
char* begin; ///< The beginning of the string
char* end; ///< One past the last character of the string
char* begin{nullptr}; ///< The beginning of the string
char* end{nullptr}; ///< One past the last character of the string

StringRefM()
: begin(nullptr)
, end(nullptr) {}
StringRefM() {}
StringRefM(int size);

operator StringRef() { return {begin, end}; }
Expand Down
2 changes: 1 addition & 1 deletion src/Nest/Utils/cppif/TypeWithStorage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct TypeWithStorage : Type {

//! @copydoc Type::changeMode
TypeWithStorage changeMode(Nest::EvalMode mode, Nest::Location loc = Nest::Location{}) const {
return TypeWithStorage(Type::changeMode(mode, loc));
return {Type::changeMode(mode, loc)};
}

//!@}
Expand Down
1 change: 1 addition & 0 deletions src/SparrowCompiler/SparrowCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ vector<Nest_CompilerModule*> gatherModules() {
return res;
}

// NOLINTNEXTLINE(bugprone-exception-escape)
int main(int argc, char* argv[]) {
CompilerStats& stats = CompilerStats::instance();
auto startTime = chrono::steady_clock::now();
Expand Down
6 changes: 4 additions & 2 deletions src/SparrowFrontend/Grammar/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ struct AstBuilder {

//! Compiler implementation or error reporter
struct CompilerErrorReporter : ErrorReporter {
CompilerErrorReporter() {
CompilerErrorReporter()
: ErrorReporter() {
self = this;
reportErrorFn = &CompilerErrorReporter::reportError;
}
Expand All @@ -92,7 +93,8 @@ struct CompilerErrorReporter : ErrorReporter {

//! Compiler implementation or error reporter
struct CompilerAstBuilder : AstBuilder {
CompilerAstBuilder() {
CompilerAstBuilder()
: AstBuilder() {
self = this;
addToNodeListFn = &CompilerAstBuilder::addToNodeList_Impl;

Expand Down
1 change: 1 addition & 0 deletions src/SparrowFrontend/Helpers/DeclsHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ Nest_NodeArray SprFrontend::expandDecls(Nest_NodeRange decls, Node* seenFrom) {
}

// Make sure we run again for the newly replaced decl
// NOLINTNEXTLINE
goto run_loop_again;
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/SparrowFrontend/Nodes/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1053,9 +1053,7 @@ ConceptDecl ConceptDecl::create(const Location& loc, StringRef name, StringRef p

StringRef ConceptDecl::paramName() const { return getCheckPropertyString("spr.paramName"); }

InstantiationsSet ConceptDecl::instantiationsSet() const {
return InstantiationsSet(children()[2]);
};
InstantiationsSet ConceptDecl::instantiationsSet() const { return {children()[2]}; };

void ConceptDecl::setContextForChildrenImpl(ConceptDecl node) {
commonSetContextForChildren(node, ContextChangeType::withSymTab);
Expand Down
Loading

0 comments on commit bbcc915

Please sign in to comment.