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

Feature: Implement a C/C++ style guide with linting #58

Open
johnlettman opened this issue Sep 2, 2023 · 3 comments
Open

Feature: Implement a C/C++ style guide with linting #58

johnlettman opened this issue Sep 2, 2023 · 3 comments

Comments

@johnlettman
Copy link
Contributor

Implementing a style guide and enforcing a uniform syntax style would significantly enhance development, particularly as the codebase expands.

Linting and Formatting Tools

Presently, there are several tools available and up to the task:

  • clang-format: A clang-based formatter that formats C and C++ code according to a style guide, such as Google, LLVM, Mozilla, Webkit, and Chromium.

    ClangFormat describes a set of tools that are built on top of LibFormat. It can support your workflow in a variety of ways including a standalone tool and editor integrations.

  • clang-tidy: A clang-based C and C++ linter tool to detect typical syntax and semantic errors.

    clang-tidy is a clang-based C++ “linter” tool. Its purpose is to provide an extensible framework for diagnosing and fixing typical programming errors, like style violations, interface misuse, or bugs that can be deduced via static analysis. clang-tidy is modular and provides a convenient interface for writing new checks.

  • cppcheck: A static analysis tool for C and C++ code to detect bugs, undefined behavior, and dangerous constructs.

    Cppcheck is a static analysis tool for C/C++ code. It provides unique code analysis to detect bugs and focuses on detecting undefined behaviour and dangerous coding constructs. The goal is to have very few false positives. Cppcheck is designed to be able to analyze your C/C++ code even if it has non-standard syntax (common in embedded projects).

  • cpplint: A C and C++ linter tool following Google's C++ style guide.

    Cpplint is a command-line tool to check C/C++ files for style issues following Google's C++ style guide. Cpplint is developed and maintained by Google Inc. at google/styleguide, also see the wikipedia entry
    While Google maintains cpplint, Google is not (very) responsive to issues and pull requests, this fork aims to be (somewhat) more open to add fixes to cpplint to enable fixes, when those fixes make cpplint usable in wider contexts. Also see discussion here Add python 3 support to cpplint and cpplint_unittest google/styleguide#528.

  • uncrustify: A source code formatter (or, "beautifier") supporting C, C++, C#, Objective-C, D, Java, Pawn, and Vala.

    A source code beautifier for C, C++, C#, Objective-C, D, Java, Pawn and Vala.

Conventions and Style Guides

Maintaining a shared programming style and implementing that standard using automation and analysis tools requires a well-formed style guide. The following major style guides exist:

Recommendation

Based on conventions in the wild, the trend seems to lean toward the Google style guide; however, I leave this personal preference to @dburkart.

Existing Style

I will now examine and compare the existing code to the listed conventions.

Brace Placement

Starting with functions, the opening brace { is on the same line as the function declaration:

bool Command::validate(const ASTNode *node) {

This alone eliminates the following style guides based on brace placement:

  • GNU: next line braces
  • Microsoft: next line braces
  • Mozilla: next line braces
  • WebKit: next line braces

This leaves the following style guides:

  • Chromium
  • Google
  • LLVM

Return Type Placement

Next, the return type of a function is on the same line as the function declaration:

bool Command::_validateDeleteHeadersCommand(const ASTNode *node) {

The following style guides also implement this preference:

  • Chromium
  • Google
  • LLVM
  • Microsoft
  • WebKit

Summary

The following style guides appear to be the most compatible:

  • Chromium
  • Google
  • LLVM

Overall Summary

Implementing clang-format, clang-tidy, and cppcheck would greatly improve QA across the codebase. These can be passively enforced via GitHub actions to contextualize pull requests.

Note

Implementing the change will also require a refactor of the existing code -- creating a particularly messy commit diff.

@johnlettman
Copy link
Contributor Author

johnlettman commented Sep 2, 2023

Currently, I am poking at a branch that uses clang-format, clang-tidy, and cppcheck with the Google Style Guide. This can also touch on #5 by tweaking parts of the build system simultaneously, given we'd be tugging on the Makefiles.

also sorry for the wall of text

@dburkart
Copy link
Owner

dburkart commented Sep 2, 2023

Starting with functions, the opening brace { is on the same line as the function declaration:

I do like next-line braces for functions, although I recognize I don't consistently do it (since I haven't been using a tool).

I think I like the WebKit style the best... but I'm probably biased from having worked on that project while at Apple...

@johnlettman
Copy link
Contributor Author

The WebKit standard is exceptionally well-defined and implements many great style choices, I don't blame you at all 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants