Skip to content

Commit

Permalink
Merge pull request #1 from jack-ullery/linter
Browse files Browse the repository at this point in the history
Add Linter
  • Loading branch information
jack-ullery authored Mar 1, 2023
2 parents 55af244 + 0d59760 commit 98c31f5
Show file tree
Hide file tree
Showing 26 changed files with 181 additions and 70 deletions.
33 changes: 33 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
Checks: 'clang-diagnostic-*,
clang-analyzer-*,
-*,
bugprone-*,
cert-*,
clang-analyzer-*,
concurrency-*,
cppcoreguidelines-*,
google-*,
hicpp-*,
linuxkernel-*,
llvm-*,
misc-*,
performance-*,
portability-*,
readability-*,
-bugprone-easily-swappable-parameters,
-readability-identifier-length,
-google-runtime-int,
-llvm-header-guard*,
-misc-const-correctness,
-misc-no-recursion'
WarningsAsErrors: ''
HeaderFilterRegex: './src/.*hh'
AnalyzeTemporaryDtors: false
FormatStyle: none
CheckOptions:
- key: misc-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic
value: 'true'

...

65 changes: 62 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,52 @@ FLEX_TARGET(LEXER

ADD_FLEX_BISON_DEPENDENCY(LEXER PARSER)

#### Linter and Static Analysis ####
find_program(CLANG_TIDY NAMES clang-tidy)
find_program(CPPCHECK NAMES cppcheck)

# If cmake was compiled with DANALYZE=TRUE
string(TOLOWER "${ANALYZE}" ANALYZE)
if("${ANALYZE}" STREQUAL "true")
message(STATUS "Adding static-analysis tools to build:")
set(USE_CLANG_TIDY TRUE)
set(USE_CPPCHECK TRUE)
unset(ANALYZE CACHE)
endif()

# If we want to use clang-tidy
if(${USE_CLANG_TIDY})
if(${CLANG_TIDY} STREQUAL "CLANG_TIDY-NOTFOUND")
message(WARNING "Could not find clang-tidy installation. Please install clang-tidy if you want to use it for static analysis.")
else()
message(STATUS "Adding clang-tidy to build")
set(CMAKE_CXX_CLANG_TIDY clang-tidy; --config-file ${CMAKE_CURRENT_BINARY_DIR}/.clang-tidy)
endif()
unset(USE_CLANG_TIDY CACHE)
endif()

add_custom_target(echo
COMMAND echo test["$<$<CONFIG:Release>:yes>"]
COMMAND_EXPAND_LISTS
)

# If we want to use cppcheck
if(${USE_CPPCHECK})
if(${CPPCHECK} STREQUAL "CPPCHECK-NOTFOUND")
message(WARNING "Could not find cppcheck installation. Please install cppcheck if you want to use it for static analysis.")
else()
message(STATUS "Adding cppcheck to build")
set(CMAKE_CXX_CPPCHECK cppcheck
--enable=warning,performance,portability,information
--suppress=missingInclude --suppress=unmatchedSuppression --suppress=internalAstError
--quiet
--inline-suppr
${SOURCES}
)
endif()
unset(USE_CPPCHECK CACHE)
endif()

#### Set Compiler Options ####
set(CMAKE_CXX_FLAGS "-g -Wall -Wextra")
set(CMAKE_CXX_STANDARD 17)
Expand All @@ -69,12 +115,25 @@ set(CMAKE_CXX_STANDARD 17)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

#### Create the library ####
add_library(${LIBRARY_NAME} ${SOURCES} ${FLEX_LEXER_OUTPUTS} ${BISON_PARSER_OUTPUT_SOURCE})
#### Compile the autogenerated code ####
add_library(autogenerated_sources OBJECT ${FLEX_LEXER_OUTPUTS} ${BISON_PARSER_OUTPUT_SOURCE})

target_include_directories(autogenerated_sources PRIVATE ${PROJECT_SOURCE_DIR}/parser)
target_include_directories(autogenerated_sources SYSTEM PRIVATE ${AUTOGEN_SOURCE_DIR})

# Disable clang-tidy and cppcheck for this target
set_target_properties(autogenerated_sources
PROPERTIES
CXX_CLANG_TIDY ""
CXX_CPPCHECK ""
)

#### Create the main library ####
add_library(${LIBRARY_NAME} ${SOURCES} $<TARGET_OBJECTS:autogenerated_sources>)

target_include_directories(${LIBRARY_NAME} PUBLIC ${PROJECT_SOURCE_DIR})
target_include_directories(${LIBRARY_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/parser)
target_include_directories(${LIBRARY_NAME} PRIVATE ${AUTOGEN_SOURCE_DIR})
target_include_directories(${LIBRARY_NAME} SYSTEM PRIVATE ${AUTOGEN_SOURCE_DIR})

# Create target to install library
install(TARGETS ${LIBRARY_NAME} DESTINATION lib)
Expand Down
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Packages needed to compile the library:
sudo apt install pkg-config cmake clang bison flex libfl-dev
```

### Testing
### Testing (Optional)
Additional packages needed to run the tests:
* GoogleTest
* GoogleMock
Expand All @@ -25,6 +25,16 @@ Additional packages needed to run the tests:
sudo apt install libgtest-dev libgmock-dev
```

### Linters and Static Analysis (Optional)
Optional packages needed to run linters and static analysis checks:
* clang-tidy
* cppcheck

#### Install commands (Ubuntu)
```
sudo apt install clang-tidy-15 cppcheck
```

## Compilation Instructions
### Prebuild
If you want to run the tests, first you must load the example profiles from the main [apparmor](https://gitlab.com/apparmor/apparmor/-/tree/master/parser/tst/simple_tests) repository. These profiles are included in a submodule for convenience.
Expand All @@ -37,6 +47,11 @@ Before you build the library, you should first generate the makefile by running:
cmake .
```

Optionally, if you want to run linters and static analysis checks:
```
cmake -DANALYZE=TRUE .
```

### Build
After the makefile is generated, you can build the library:
```
Expand Down
4 changes: 3 additions & 1 deletion src/apparmor_file_rule.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include <utility>

#include "apparmor_file_rule.hh"
#include "parser/tree/FileNode.hh"

AppArmor::FileRule::FileRule(std::shared_ptr<FileNode> model)
: model{model}
: model{std::move(model)}
{ }

std::string AppArmor::FileRule::getFilename() const
Expand Down
4 changes: 2 additions & 2 deletions src/apparmor_file_rule.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace AppArmor {
class FileRule {
public:
FileRule() = default;
FileRule(std::shared_ptr<FileNode> model);
explicit FileRule(std::shared_ptr<FileNode> model);

std::string getFilename() const;
std::string getFilemode() const;
Expand All @@ -21,6 +21,6 @@ namespace AppArmor {
private:
std::shared_ptr<FileNode> model;
};
}
} // namespace AppArmor

#endif // APPARMOR_FILE_RULE_HH
4 changes: 2 additions & 2 deletions src/apparmor_parser.hh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ParseTree;
namespace AppArmor {
class Parser {
public:
Parser(std::ifstream &stream);
explicit Parser(std::ifstream &stream);

std::list<Profile> getProfileList() const;

Expand All @@ -20,6 +20,6 @@ namespace AppArmor {

std::list<Profile> profile_list;
};
}
} // namespace AppArmor

#endif // APPARMOR_PARSER_HH
4 changes: 2 additions & 2 deletions src/apparmor_profile.hh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ProfileNode;
namespace AppArmor {
class Profile {
public:
Profile(std::shared_ptr<ProfileNode> profile_model);
explicit Profile(std::shared_ptr<ProfileNode> profile_model);

// Returns the name of this profile
std::string name() const;
Expand All @@ -26,6 +26,6 @@ namespace AppArmor {
private:
std::shared_ptr<ProfileNode> profile_model;
};
}
} // namespace AppArmor

#endif // APPARMOR_PROFILE_HH
7 changes: 4 additions & 3 deletions src/parser/lexer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@

class Lexer : public yyFlexLexer {
public:
Lexer(std::istream& arg_yyin)
explicit Lexer(std::istream& arg_yyin)
: yyFlexLexer(arg_yyin, std::cout) {}

Lexer(std::istream& arg_yyin, std::ostream& arg_yyout)
: yyFlexLexer(arg_yyin, arg_yyout) {}


// NOLINTNEXTLINE
virtual symbol_type yylex(Driver& driver);
};

Expand Down
4 changes: 3 additions & 1 deletion src/parser/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
#include "parser.h"

#include <ctype.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>

#include <sys/stat.h>
#include <sys/types.h>

//NOLINTBEGIN
/**
* isodigit - test if a character is an octal digit
* @c: character to test
Expand Down Expand Up @@ -179,3 +180,4 @@ int strn_escseq(const char **pos, const char *chrs, size_t n)
pos--;
return -1;
}
//NOLINTEND
18 changes: 10 additions & 8 deletions src/parser/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,23 @@

/* assistance routines */

#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <cassert>
#include <cctype>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <linux/capability.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>

#include "lib.h"
#include "common.hh"
#include "lexer.hh"

// NOLINTBEGIN
/* #define DEBUG */
#ifdef DEBUG
#undef PDEBUG
Expand Down Expand Up @@ -230,3 +231,4 @@ char *processid(const char *string, int len)
return processquoted(string, len);
return processunquoted(string, len);
}
// NOLINTEND
2 changes: 1 addition & 1 deletion src/parser/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
#define __AA_PARSER_H

#include <endian.h>
#include <string.h>
#include <stdint.h>
#include <string.h>
#include <sys/resource.h>

#include <libintl.h>
Expand Down
4 changes: 2 additions & 2 deletions src/parser/tree/AbstractionNode.hh
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class AbstractionNode : public RuleNode {
std::string getPath();

private:
virtual operator std::string() const;
virtual explicit operator std::string() const;

std::string path;
bool is_if_exists;
bool is_if_exists = false;
};

#endif // ABSTRACTION_NODE_HH
3 changes: 1 addition & 2 deletions src/parser/tree/AliasNode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
#include <sstream>

AliasNode::AliasNode(const std::string &from, const std::string &to)
: TreeNode(),
from{from},
: from{from},
to{to}
{ }

Expand Down
2 changes: 1 addition & 1 deletion src/parser/tree/AliasNode.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class AliasNode : public TreeNode {
AliasNode(const std::string &from, const std::string &to);

private:
virtual operator std::string() const;
virtual explicit operator std::string() const;
std::string from;
std::string to;
};
Expand Down
2 changes: 1 addition & 1 deletion src/parser/tree/FileNode.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class FileNode : public RuleNode {
std::string getFilemode() const;

private:
bool isSubset;
bool isSubset = false;
std::string filename;
std::string exec_target;
std::string fileMode;
Expand Down
4 changes: 2 additions & 2 deletions src/parser/tree/LinkNode.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ class LinkNode : public RuleNode {
LinkNode() = default;
LinkNode(uint64_t startPos, uint64_t stopPos, bool isSubset, const std::string &linkFrom, const std::string &linkTo);

virtual operator std::string() const;
virtual explicit operator std::string() const;

private:
bool isSubset;
bool isSubset = false;
std::string from;
std::string to;
};
Expand Down
6 changes: 4 additions & 2 deletions src/parser/tree/ParseTree.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <utility>

#include "ParseTree.hh"
#include "TreeNode.hh"

ParseTree::ParseTree(TreeNode preamble, std::shared_ptr<std::list<ProfileNode>> profileList)
: preamble{preamble},
profileList{profileList}
: preamble{std::move(preamble)},
profileList{std::move(profileList)}
{ }
3 changes: 1 addition & 2 deletions src/parser/tree/PrefixNode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
#include "tree/TreeNode.hh"

PrefixNode::PrefixNode(bool audit, bool should_deny, bool owner)
: TreeNode(),
audit{audit},
: audit{audit},
should_deny{should_deny},
owner{owner}
{ }
2 changes: 1 addition & 1 deletion src/parser/tree/PrefixNode.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class PrefixNode : public TreeNode {
public:
PrefixNode(bool audit = DEFAULT_AUDIT, bool should_deny = DEFAULT_PERM_MODE, bool owner = DEFAULT_OWNER);
explicit PrefixNode(bool audit = DEFAULT_AUDIT, bool should_deny = DEFAULT_PERM_MODE, bool owner = DEFAULT_OWNER);

static constexpr bool DEFAULT_AUDIT = false;
static constexpr bool DEFAULT_PERM_MODE = false;
Expand Down
2 changes: 1 addition & 1 deletion src/parser/tree/ProfileNode.hh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ProfileNode : public TreeNode {

RuleList<ProfileNode> getRules();

protected:
private:
RuleList<ProfileNode> rules;
};

Expand Down
Loading

0 comments on commit 98c31f5

Please sign in to comment.