diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 000000000..02bb3916f --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,6 @@ +--- +Checks: '-*,google-*,cppcoreguidelines-*,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-pro-bounds-*,openmp-*,performance-*,portability-*,modernize-*,-modernize-use-trailing-*' +WarningsAsErrors: '' +HeaderFilterRegex: '' +AnalyzeTemporaryDtors: false +FormatStyle: '{ BasedOnStyle: Google, UseTab: Never, IndentWidth: 4, TabWidth: 4, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: true, ColumnLimit: 80, AccessModifierOffset: -3, AlignConsecutiveMacros: true }' diff --git a/.github/workflows/awesome_workflow.yml b/.github/workflows/awesome_workflow.yml index f8feb1d3e..2e0792e17 100644 --- a/.github/workflows/awesome_workflow.yml +++ b/.github/workflows/awesome_workflow.yml @@ -16,7 +16,9 @@ jobs: - name: requirements run: | sudo apt -qq -y update - sudo apt -qq install clang-format + sudo apt -qq install clang-tidy-10 + # checks are passing with less errors when used with this version. + # The default installs v6.0 which did not work out well in my tests - name: Setup Git Specs run: | git config --global user.name github-actions @@ -43,18 +45,6 @@ jobs: fi done git commit -am "formatting filenames $GITHUB_SHA" || true - - name: Clang Formatter - run: | - for fname in $(find . -name '*.cpp' -o -name '*.h') - do - clang-format --verbose -i --style="$line1 $line2 $line3 $line4" "$fname" - done - git commit -am "formatting source-code for $GITHUB_SHA" || true - env: - line1: "{ BasedOnStyle: Google, UseTab: Never," - line2: "IndentWidth: 4, TabWidth: 4, " - line3: "AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false," - line4: "ColumnLimit: 80, AccessModifierOffset: -3 }" - name: Update DIRECTORY.md shell: python @@ -100,24 +90,21 @@ jobs: with open("DIRECTORY.md", "w") as out_file: out_file.write(build_directory_md(".") + "\n") - - name: Update DIRECTORY.md + - name: Commit DIRECTORY.md + run: git commit -m "updating DIRECTORY.md" DIRECTORY.md || true + - name: Get file changes run: | - cat DIRECTORY.md - git config --global user.name github-actions - git config --global user.email '${GITHUB_ACTOR}@users.noreply.github.com' - git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY - git add DIRECTORY.md - git commit -am "updating DIRECTORY.md" || true - git push --force origin HEAD:$GITHUB_REF || true - - name: Install CPPLINT - run: | - python -m pip install cpplint git remote -v git branch git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY git diff --diff-filter=dr --name-only origin/master > git_diff.txt echo "Files changed-- `cat git_diff.txt`" - - name: cpplint_modified_files + - name: Configure for static lint checks + # compiling first gives clang-tidy access to all the header files and settings used to compile the programs. + # This will check for macros, if any, on linux and not for Windows. But the use of portability checks should + # be able to catch any errors for other platforms. + run: cmake -B build -S . -DCMAKE_EXPORT_COMPILE_COMMANDS=ON + - name: Lint modified files shell: python run: | import os @@ -135,9 +122,9 @@ jobs: if not cpp_files: sys.exit(0) - print("cpplint:") for cpp_file in cpp_files: - subprocess.run(["cpplint", "--filter=-legal/copyright,-build/include", cpp_file], check=True, text=True) + subprocess.run(["clang-tidy-10", "--fix", "-p=build", cpp_file, "--"], + check=True, text=True, stderr=subprocess.STDOUT) # print("g++:") # compile_exts = tuple(".c .c++ .cc .cpp .cu .cxx".split()) @@ -163,6 +150,11 @@ jobs: bad_files = len(upper_files + space_files + nodir_files) if bad_files: sys.exit(bad_files) + - name: Commit and push changes + run: | + git diff DIRECTORY.md + git commit -am "clang-tidy fixes for $GITHUB_SHA" || true + git push --force origin HEAD:$GITHUB_REF || true build: name: Compile checks diff --git a/.gitpod.yml b/.gitpod.yml index 40750258c..ece107997 100644 --- a/.gitpod.yml +++ b/.gitpod.yml @@ -7,6 +7,7 @@ github: addComment: false addCheck: false master: true + branches: true pullRequestsFromForks: true vscode: diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9f0205bfd..35d875d43 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -28,8 +28,85 @@ We are very happy that you consider implementing algorithms and data structures - Strictly use snake_case (underscore_separated) in filenames. - If you have added or modified code, please make sure the code compiles before submitting. - Our automated testing runs [__cpplint__](https://github.com/cpplint/cpplint) on all pull requests so please be sure that your code passes before submitting. +- Please conform to [doxygen](https://www.doxygen.nl/manual/docblocks.html) standard and document the code as much as possible. This not only facilitates the readers but also generates the correct info on website. - **Be consistent in use of these guidelines.** +#### Documentation +- Make sure you put useful comments in your code. Do not comment things that are obvious. +- Please avoid creating new directories if at all possible. Try to fit your work into the existing directory structure. If you want to create a new directory, then please check if a similar category has been recently suggested or created by other pull requests. +- If you have modified/added documentation, please ensure that your language is concise and contains no grammar errors. +- Do not update README.md along with other changes, first create an issue and then link to that issue in your pull request to suggest specific changes required to README.md +- The repository follows [Doxygen](https://www.doxygen.nl/manual/docblocks.html) standards and auto-generates the [repo website](https://thealgorithms.github.io/C-Plus-Plus). Please ensure the code is documented in this structure. Sample implementation is given below. + +#### Test +- Make sure to add examples and test cases in your main() function. +- If you find any algorithm or document without tests, please feel free to create a pull request or issue describing suggested changes. +- Please try to add one or more `test()` functions that will invoke the algorithm implementation on random test data with expected output. Use `assert()` function to confirm that the tests will pass. + +#### Typical structure of a program: +```cpp +/** + * @file + * @brief Add one line description here + * @details + * This is a multi line + * description containing links, references, + * math equations, etc + * @author [Name](https://github.com/handle) + * @see related_file.cpp, another_file.cpp + */ + +#include + +/** + * @namespace + */ +namespace name { + +/** + * Class documentation + */ +class cls_name{ + private: + int var1; ///< short info of this variable + char *msg; ///< short info + + public: + // other members also documented as below +} + +/** + * Function documentation + * @tparam T this is a one-line info about T + * @param param1 on-line info about param1 + * @param param2 on-line info about param2 + * @returns `true` if ... + * @returns `false` if ... + */ +template +bool func(int param1, T param2) { + // function statements here + if(/*something bad*/) + return false; + + return true; +} + +/** Test function */ +void test() { + /* some statements */ + assert(func(...) == ...); // this ensures that the algorithm works as expected + + // can have multiple checks +} + +/** Main function */ +int main(int argc, char *argv[]) { + // code here + return 0; +} +``` + #### New File Name guidelines - Use lowercase words with ``"_"`` as separator - For instance @@ -70,16 +147,6 @@ Common prefixes: - docs: Documentation changes - test: Correct existing tests or add new ones -#### Documentation -- Make sure you put useful comments in your code. Do not comment things that are obvious. -- Please avoid creating new directories if at all possible. Try to fit your work into the existing directory structure. If you want to create a new directory, then please check if a similar category has been recently suggested or created by other pull requests. -- If you have modified/added documentation, please ensure that your language is concise and contains no grammar errors. -- Do not update README.md along with other changes, first create an issue and then link to that issue in your pull request to suggest specific changes required to README.md - -#### Test -- Make sure to add examples and test cases in your main() function. -- If you find any algorithm or document without tests, please feel free to create a pull request or issue describing suggested changes. - ### Pull Requests - Checkout our [pull request template](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/.github/pull_request_template.md) diff --git a/math/sieve_of_eratosthenes.cpp b/math/sieve_of_eratosthenes.cpp index d8fa70531..e30bc1891 100644 --- a/math/sieve_of_eratosthenes.cpp +++ b/math/sieve_of_eratosthenes.cpp @@ -10,25 +10,21 @@ * @see primes_up_to_billion.cpp prime_numbers.cpp */ -#include - -/** Maximum number of primes */ -#define MAX 10000000 - -/** array to store the primes */ -bool isprime[MAX]; +#include // for io operations /** * This is the function that finds the primes and eliminates * the multiples. + * @param N number of primes to check + * @param [out] isprime a boolean array of size `N` identifying if `i`^th number is prime or not */ -void sieve(uint32_t N) { - isprime[0] = false; - isprime[1] = false; - for (uint32_t i = 2; i <= N; i++) { - if (isprime[i]) { - for (uint32_t j = (i << 1); j <= N; j += i) { - isprime[j] = false; +void sieve(uint32_t N, bool *isprime) { + isprime[0] = true; + isprime[1] = true; + for (uint32_t i = 2; i * i <= N; i++) { + if (!isprime[i]) { + for (uint32_t j = (i << 1); j <= N; j = j + i) { + isprime[j] = true; } } } @@ -36,10 +32,12 @@ void sieve(uint32_t N) { /** * This function prints out the primes to STDOUT + * @param N number of primes to check + * @param [in] isprime a boolean array of size `N` identifying if `i`^th number is prime or not */ -void print(uint32_t N) { - for (uint32_t i = 1; i <= N; i++) { - if (isprime[i]) { +void print(uint32_t N, const bool *isprime) { + for (uint32_t i = 2; i <= N; i++) { + if (!isprime[i]) { std::cout << i << ' '; } } @@ -47,19 +45,14 @@ void print(uint32_t N) { } /** - * Initialize the array + * Main function */ -void init() { - for (uint32_t i = 1; i < MAX; i++) { - isprime[i] = true; - } -} - -/** main function */ int main() { uint32_t N = 100; - init(); - sieve(N); - print(N); + bool *isprime = new bool[N]; + sieve(N, isprime); + print(N, isprime); + delete[] isprime; + return 0; }