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

Minor code optimization #7

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/ThreadPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Modified version of ThreadPool (https://github.com/progschj/ThreadPool/blob/master/ThreadPool.h)
* Changes
* - Replaced std::result_of with std::invoke_result_t for c++20
* - Added reserve call for workers to preallocate threads
*/

#include <vector>
Expand Down Expand Up @@ -39,6 +40,7 @@ class ThreadPool {
inline ThreadPool::ThreadPool(size_t threads)
: stop(false)
{
workers.reserve(threads);
matyalatte marked this conversation as resolved.
Show resolved Hide resolved
for(size_t i = 0;i<threads;++i)
workers.emplace_back(
[this]
Expand Down
16 changes: 8 additions & 8 deletions include/cleanse.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,20 @@ class CleansedLines {
*/
std::string ReplaceAlternateTokens(const std::string& line);

size_t NumLines() const { return m_lines.size(); }
[[nodiscard]] size_t NumLines() const { return m_lines.size(); }

const std::string& GetLineAt(size_t id) const { return m_lines[id]; }
const std::string& GetElidedAt(size_t id) const { return m_elided[id]; }
const std::vector<std::string>& GetElidedLines() const {
[[nodiscard]] const std::string& GetLineAt(size_t id) const { return m_lines[id]; }
[[nodiscard]] const std::string& GetElidedAt(size_t id) const { return m_elided[id]; }
[[nodiscard]] const std::vector<std::string>& GetElidedLines() const {
return m_elided;
}
const std::string& GetRawLineAt(size_t id) const { return m_raw_lines[id]; }
const std::string& GetLineWithoutRawStringAt(size_t id) const {
[[nodiscard]] const std::string& GetRawLineAt(size_t id) const { return m_raw_lines[id]; }
[[nodiscard]] const std::string& GetLineWithoutRawStringAt(size_t id) const {
return m_lines_without_raw_strings[id];
}
const std::vector<std::string>& GetLinesWithoutRawStrings() const {
[[nodiscard]] const std::vector<std::string>& GetLinesWithoutRawStrings() const {
return m_lines_without_raw_strings;
}

bool HasComment(size_t id) const { return m_has_comment[id]; }
[[nodiscard]] bool HasComment(size_t id) const { return m_has_comment[id]; }
};
20 changes: 10 additions & 10 deletions include/cpplint_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class CppLintState {
public:
CppLintState();

int OutputFormat() const { return m_output_format; }
[[nodiscard]] int OutputFormat() const { return m_output_format; }
void SetOutputFormat(const std::string& output_format) {
// Sets the output format for errors.
if (output_format == "vs7")
Expand All @@ -74,15 +74,15 @@ class CppLintState {
m_output_format = OUTPUT_EMACS;
}

bool Quiet() const { return m_quiet; }
[[nodiscard]] bool Quiet() const { return m_quiet; }
bool SetQuiet(bool quiet) {
// Sets the module's quiet settings, and returns the previous setting.
bool last_quiet = m_quiet;
m_quiet = quiet;
return last_quiet;
}

int VerboseLevel() const { return m_verbose_level; }
[[nodiscard]] int VerboseLevel() const { return m_verbose_level; }
int SetVerboseLevel(int level) {
// Sets the module's verbosity, and returns the previous setting.
int last_verbose_level = m_verbose_level;
Expand All @@ -106,11 +106,11 @@ class CppLintState {
m_errors_by_category.clear();
}

int ErrorCount() const { return m_error_count; }
int ErrorCount(const std::string& category) const;
[[nodiscard]] int ErrorCount() const { return m_error_count; }
[[nodiscard]] int ErrorCount(const std::string& category) const;

void SetNumThreads(int num_threads) { m_num_threads = num_threads; }
int GetNumThreads() const { return m_num_threads; }
[[nodiscard]] int GetNumThreads() const { return m_num_threads; }

// Bumps the module's error statistic.
void IncrementErrorCount(const std::string& category);
Expand All @@ -129,10 +129,10 @@ class CppLintState {

// Print a summary of errors by category, and the total.
void PrintErrorCounts();
void PrintInfo(const std::string& message);
void PrintError(const std::string& message);
void PrintInfo(const std::string& message) const;
void PrintError(const std::string& message) const;

bool AddJUnitFailure(const std::string& filename,
static bool AddJUnitFailure(const std::string& filename,
size_t linenum,
const std::string& message,
const std::string& category,
Comment on lines +135 to 138
Copy link
Owner

Choose a reason for hiding this comment

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

You still need to align arguments for some functions that use static and [[nodiscard]].

Expand All @@ -145,5 +145,5 @@ class CppLintState {
return false;
}

std::string FormatJUnitXML() { return ""; }
static std::string FormatJUnitXML() { return ""; }
};
8 changes: 4 additions & 4 deletions include/error_suppressions.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,19 @@ class LineRange {
m_end = end;
}

std::string ToStr() const {
[[nodiscard]] std::string ToStr() const {
return "[" + std::to_string(m_begin) + "-" + std::to_string(m_end) +"]";
}

bool Contain(size_t linenum) const {
[[nodiscard]] bool Contain(size_t linenum) const {
return m_begin <= linenum && linenum <= m_end;
}

bool ContainRange(const LineRange& other) const {
[[nodiscard]] bool ContainRange(const LineRange& other) const {
return m_begin <= other.m_begin && other.m_end <= m_end;
}

size_t GetBegin() {
[[nodiscard]] size_t GetBegin() const {
return m_begin;
}

Expand Down
12 changes: 6 additions & 6 deletions include/file_linter.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class FileLinter {
bool m_has_error;

public:
FileLinter() {}
FileLinter() = default;
FileLinter(const fs::path& file, CppLintState* state, const Options& options) :
m_cpplint_state(state),
m_options(options),
Expand All @@ -48,8 +48,8 @@ class FileLinter {
m_re_result(RegexCreateMatchData(16)),
m_has_error(false) {}

fs::path GetRelativeFromRepository(const fs::path& file, const fs::path& repository);
fs::path GetRelativeFromSubdir(const fs::path& file, const fs::path& subdir);
static fs::path GetRelativeFromRepository(const fs::path& file, const fs::path& repository);
static fs::path GetRelativeFromSubdir(const fs::path& file, const fs::path& subdir);

// Logs an error if no Copyright message appears at the top of the file.
void CheckForCopyright(const std::vector<std::string>& lines);
Expand All @@ -69,9 +69,9 @@ class FileLinter {
*/
void CheckForHeaderGuard(const CleansedLines& clean_lines);

bool IsForwardClassDeclaration(const std::string& elided_line);
static bool IsForwardClassDeclaration(const std::string& elided_line);

bool IsMacroDefinition(const CleansedLines& clean_lines,
static bool IsMacroDefinition(const CleansedLines& clean_lines,
const std::string& elided_line, size_t linenum);
Comment on lines +74 to 75
Copy link
Owner

Choose a reason for hiding this comment

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

Arguments should be aligned.


void CheckForNamespaceIndentation(const CleansedLines& clean_lines,
Expand Down Expand Up @@ -231,7 +231,7 @@ class FileLinter {
IncludeState* include_state);

// Checks whether where function type arguments are expected.
bool ExpectingFunctionArgs(const CleansedLines& clean_lines,
static bool ExpectingFunctionArgs(const CleansedLines& clean_lines,
const std::string& elided_line, size_t linenum);
Comment on lines +234 to 235
Copy link
Owner

Choose a reason for hiding this comment

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

Arguments should be aligned.


// Checks for a C-style cast by looking for the pattern.
Expand Down
49 changes: 22 additions & 27 deletions include/nest_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class BlockInfo {
m_check_namespace_indentation(false),
m_block_type(BLOCK_INFO) {}

virtual ~BlockInfo() {}
virtual ~BlockInfo() = default;

/* Run checks that applies to text up to the opening brace.

Expand Down Expand Up @@ -75,21 +75,21 @@ class BlockInfo {
This is convenient for verifying that an object is an instance of
a _BlockInfo, but not an instance of any of the derived classes.
*/
bool IsBlockInfo() const { return m_block_type == BLOCK_INFO; }
bool IsExternCInfo() const { return m_block_type == EXTERN_C_INFO; }
bool IsClassInfo() const { return m_block_type == CLASS_INFO; }
bool IsNamespaceInfo() const { return m_block_type == NAMESPACE_INFO; }
[[nodiscard]] bool IsBlockInfo() const { return m_block_type == BLOCK_INFO; }
[[nodiscard]] bool IsExternCInfo() const { return m_block_type == EXTERN_C_INFO; }
[[nodiscard]] bool IsClassInfo() const { return m_block_type == CLASS_INFO; }
[[nodiscard]] bool IsNamespaceInfo() const { return m_block_type == NAMESPACE_INFO; }

bool SeenOpenBrace() const { return m_seen_open_brace; }
[[nodiscard]] bool SeenOpenBrace() const { return m_seen_open_brace; }
void SetSeenOpenBrace(bool seen_open_brace) { m_seen_open_brace = seen_open_brace; }

int OpenParentheses() const { return m_open_parentheses; }
[[nodiscard]] int OpenParentheses() const { return m_open_parentheses; }
void IncOpenParentheses(int inc) { m_open_parentheses += inc; }

int InlineAsm() const { return m_inline_asm; }
[[nodiscard]] int InlineAsm() const { return m_inline_asm; }
void SetInlineAsm(int inline_asm) { m_inline_asm = inline_asm; }

size_t StartingLinenum() const { return m_starting_linenum; }
[[nodiscard]] size_t StartingLinenum() const { return m_starting_linenum; }
};

// Stores information about an 'extern "C"' block.
Expand Down Expand Up @@ -124,13 +124,13 @@ class ClassInfo : public BlockInfo {
void CheckEnd(const CleansedLines& clean_lines,
size_t linenum,
FileLinter* file_linter) override;
const std::string& Access() const { return m_access; }
[[nodiscard]] const std::string& Access() const { return m_access; }
void SetAccess(const std::string& access) { m_access = access; }
bool IsStruct() const { return m_is_struct; }
size_t ClassIndent() const { return m_class_indent; }
const std::string& Name() const { return m_name; }
const std::string& Basename() const { return m_basename; }
size_t LastLine() { return m_last_line; }
[[nodiscard]] bool IsStruct() const { return m_is_struct; }
[[nodiscard]] size_t ClassIndent() const { return m_class_indent; }
[[nodiscard]] const std::string& Name() const { return m_name; }
[[nodiscard]] const std::string& Basename() const { return m_basename; }
[[nodiscard]] size_t LastLine() const { return m_last_line; }
};

// Stores information about a namespace.
Expand All @@ -140,9 +140,9 @@ class NamespaceInfo : public BlockInfo {

public:
NamespaceInfo(const std::string& name, size_t linenum) :
BlockInfo(linenum, false) {
BlockInfo(linenum, false),
m_name(name) {
m_block_type = NAMESPACE_INFO;
m_name = name;
m_check_namespace_indentation = true;
}

Expand All @@ -159,18 +159,13 @@ class PreprocessorInfo {
std::vector<BlockInfo*> m_stack_before_else;

public:
explicit PreprocessorInfo(const std::vector<BlockInfo*>& stack_before_if) {
// The entire nesting stack before #if
m_stack_before_if = stack_before_if;

// The entire nesting stack up to #else
m_stack_before_else = {};

// Whether we have already seen #else or #elif
m_seen_else = false;
explicit PreprocessorInfo(const std::vector<BlockInfo*>& stack_before_if) :
m_stack_before_if(stack_before_if), // The entire nesting stack before #if
m_stack_before_else({}), // The entire nesting stack up to #else
m_seen_else(false) { // Whether we have already seen #else or #elif
}

bool SeenElse() { return m_seen_else; }
[[nodiscard]] bool SeenElse() const { return m_seen_else; }
void SetSeenElse(bool seen_else) { m_seen_else = seen_else; }

const std::vector<BlockInfo*>& StackBeforeIf() {
Expand Down
38 changes: 20 additions & 18 deletions include/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class Filter {
public:
Filter() :
m_sign(false),
m_category(""),
m_file(""),
m_category(),
m_file(),
m_linenum(INDEX_NONE) {}

explicit Filter(const std::string& filter) {
Expand All @@ -27,9 +27,9 @@ class Filter {

void ParseFilterSelector(const std::string& filter);

bool IsPositive() const { return m_sign; }
[[nodiscard]] bool IsPositive() const { return m_sign; }

bool IsMatched(const std::string& category,
[[nodiscard]] bool IsMatched(const std::string& category,
const std::string& file,
size_t linenum) const {
Comment on lines +32 to 34
Copy link
Owner

Choose a reason for hiding this comment

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

Arguments should be aligned.

return category.starts_with(m_category) &&
Expand Down Expand Up @@ -76,12 +76,13 @@ class Options {
// Searches a list of filenames and replaces directories in the list with
// all files descending from those directories. Files with extensions not in
// the valid extensions list are excluded.
std::vector<fs::path> ExpandDirectories(const std::vector<fs::path>& filenames);
std::vector<fs::path> ExpandDirectories(const std::vector<fs::path>& filenames) const;

// Filters out files listed in the --exclude command line switch. File paths
// in the switch are evaluated relative to the current working directory
std::vector<fs::path> FilterExcludedFiles(std::vector<fs::path> filenames,
const std::vector<fs::path>& excludes);
static std::vector<fs::path> FilterExcludedFiles(
std::vector<fs::path> filenames,
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
std::vector<fs::path> filenames,
std::vector<fs::path> filenames,

Tab indentations should be removed.

const std::vector<fs::path>& excludes);

public:
Options() :
Expand All @@ -102,28 +103,29 @@ class Options {
std::vector<fs::path> ParseArguments(int argc, char** argv,
CppLintState* cpplint_state);

const fs::path& Root() const { return m_root; }
const fs::path& Repository() const { return m_repository; }
size_t LineLength() const { return m_line_length; }
[[nodiscard]] const fs::path& Root() const { return m_root; }
[[nodiscard]] const fs::path& Repository() const { return m_repository; }
[[nodiscard]] size_t LineLength() const { return m_line_length; }

std::set<std::string> GetAllExtensions() const;
std::set<std::string> GetHeaderExtensions() const;
[[nodiscard]] std::set<std::string> GetAllExtensions() const;
[[nodiscard]] std::set<std::string> GetHeaderExtensions() const;

bool ProcessConfigOverrides(const fs::path& filename,
CppLintState* cpplint_state);

void PrintUsage(const std::string& message = "");
void PrintUsage(const std::string& message = "") const;

int IncludeOrder() const { return m_include_order; }
[[nodiscard]] int IncludeOrder() const { return m_include_order; }

const std::vector<Filter>& Filters() const { return m_filters; }
[[nodiscard]] const std::vector<Filter>& Filters() const { return m_filters; }

// Adds filters to the existing list of error-message filters.
bool AddFilters(const std::string& filters);

// Checks if the error is filtered or not.
bool ShouldPrintError(const std::string& category,
const std::string& filename, size_t linenum) const;
[[nodiscard]] bool ShouldPrintError(
const std::string& category,
const std::string& filename, size_t linenum) const;

bool Timing() const { return m_timing; }
[[nodiscard]] bool Timing() const { return m_timing; }
};
2 changes: 1 addition & 1 deletion include/regex_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ inline void RegexReplace(const regex_code& regex, const std::string& fmt,

std::string RegexEscape(const std::string& str);

inline std::string RegexEscape(const std::string_view& str) {
inline std::string RegexEscape(std::string_view str) {
return RegexEscape(std::string(str));
}

Expand Down
Loading
Loading