Skip to content

Commit

Permalink
Add clang-format and clang-tidy
Browse files Browse the repository at this point in the history
clang-format added

clang-tidy changes

Add .clang-tidy, update project files

Add CONTRIBUTING.md
  • Loading branch information
rudychung committed Nov 3, 2022
1 parent 6db4442 commit 7471168
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 62 deletions.
16 changes: 16 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Format Style Options - Created with Clang Power Tools
---
AlignAfterOpenBracket: Align
AlignOperands: DontAlign
BreakBeforeBraces: Attach
BreakStringLiterals: false
ColumnLimit: 150
Cpp11BracedListStyle: true
IndentWidth: 4
PointerAlignment: Left # Right
ReflowComments: false
SortIncludes: true
SpaceBeforeParens: ControlStatements
TabWidth: 4
UseTab: Always
...
7 changes: 7 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
Checks: '-*,boost-use-to-string,bugprone-argument-comment,bugprone-assert-side-effect,bugprone-bad-signal-to-kill-thread,bugprone-bool-pointer-implicit-conversion,bugprone-copy-constructor-init,bugprone-dangling-handle,bugprone-dynamic-static-initializers,bugprone-fold-init-type,bugprone-forward-declaration-namespace,bugprone-forwarding-reference-overload,bugprone-inaccurate-erase,bugprone-incorrect-roundings,bugprone-infinite-loop,bugprone-integer-division,bugprone-lambda-function-name,bugprone-macro-parentheses,bugprone-macro-repeated-side-effects,bugprone-misplaced-operator-in-strlen-in-alloc,bugprone-misplaced-pointer-arithmetic-in-alloc,bugprone-misplaced-widening-cast,bugprone-move-forwarding-reference,bugprone-multiple-statement-macro,bugprone-narrowing-conversions,bugprone-no-escape,bugprone-not-null-terminated-result,bugprone-parent-virtual-call,bugprone-posix-return,bugprone-redundant-branch-condition,bugprone-reserved-identifier,bugprone-signal-handler,bugprone-signed-char-misuse,bugprone-sizeof-container,bugprone-sizeof-expression,bugprone-spuriously-wake-up-functions,bugprone-string-constructor,bugprone-string-integer-assignment,bugprone-string-literal-with-embedded-nul,bugprone-suspicious-enum-usage,bugprone-suspicious-include,bugprone-suspicious-memset-usage,bugprone-suspicious-missing-comma,bugprone-suspicious-semicolon,bugprone-suspicious-string-compare,bugprone-swapped-arguments,bugprone-terminating-continue,bugprone-throw-keyword-missing,bugprone-too-small-loop-variable,bugprone-undefined-memory-manipulation,bugprone-undelegated-constructor,bugprone-unhandled-self-assignment,bugprone-unused-raii,bugprone-unused-return-value,bugprone-use-after-move,bugprone-virtual-near-miss,clang-analyzer-apiModeling.StdCLibraryFunctions,clang-analyzer-apiModeling.TrustNonnull,clang-analyzer-apiModeling.google.GTest,clang-analyzer-apiModeling.llvm.CastValue,clang-analyzer-apiModeling.llvm.ReturnValue,clang-analyzer-core.CallAndMessage,clang-analyzer-core.CallAndMessageModeling,clang-analyzer-core.DivideZero,clang-analyzer-core.DynamicTypePropagation,clang-analyzer-core.NonNullParamChecker,clang-analyzer-core.NonnilStringConstants,clang-analyzer-core.NullDereference,clang-analyzer-core.StackAddrEscapeBase,clang-analyzer-core.StackAddressEscape,clang-analyzer-core.UndefinedBinaryOperatorResult,clang-analyzer-core.VLASize,clang-analyzer-core.builtin.BuiltinFunctions,clang-analyzer-core.builtin.NoReturnFunctions,clang-analyzer-core.uninitialized.ArraySubscript,clang-analyzer-core.uninitialized.Assign,clang-analyzer-core.uninitialized.Branch,clang-analyzer-core.uninitialized.CapturedBlockVariable,clang-analyzer-core.uninitialized.UndefReturn,clang-analyzer-cplusplus.InnerPointer,clang-analyzer-cplusplus.Move,clang-analyzer-cplusplus.NewDelete,clang-analyzer-cplusplus.NewDeleteLeaks,clang-analyzer-cplusplus.PlacementNew,clang-analyzer-cplusplus.PureVirtualCall,clang-analyzer-cplusplus.SelfAssignment,clang-analyzer-cplusplus.SmartPtrModeling,clang-analyzer-cplusplus.VirtualCallModeling,clang-analyzer-deadcode.DeadStores,clang-analyzer-fuchsia.HandleChecker,clang-analyzer-nullability.NullPassedToNonnull,clang-analyzer-nullability.NullReturnedFromNonnull,clang-analyzer-nullability.NullabilityBase,clang-analyzer-nullability.NullableDereferenced,clang-analyzer-nullability.NullablePassedToNonnull,clang-analyzer-nullability.NullableReturnedFromNonnull,clang-analyzer-optin.cplusplus.UninitializedObject,clang-analyzer-optin.cplusplus.VirtualCall,clang-analyzer-optin.mpi.MPI-Checker,clang-analyzer-optin.osx.OSObjectCStyleCast,clang-analyzer-optin.osx.cocoa.localizability.EmptyLocalizationContextChecker,clang-analyzer-optin.osx.cocoa.localizability.NonLocalizedStringChecker,clang-analyzer-optin.performance.GCDAntipattern,clang-analyzer-optin.performance.Padding,clang-analyzer-optin.portability.UnixAPI,clang-analyzer-osx.API,clang-analyzer-osx.MIG,clang-analyzer-osx.NSOrCFErrorDerefChecker,clang-analyzer-osx.NumberObjectConversion,clang-analyzer-osx.OSObjectRetainCount,clang-analyzer-osx.ObjCProperty,clang-analyzer-osx.SecKeychainAPI,clang-analyzer-osx.cocoa.AtSync,clang-analyzer-osx.cocoa.AutoreleaseWrite,clang-analyzer-osx.cocoa.ClassRelease,clang-analyzer-osx.cocoa.Dealloc,clang-analyzer-osx.cocoa.IncompatibleMethodTypes,clang-analyzer-osx.cocoa.Loops,clang-analyzer-osx.cocoa.MissingSuperCall,clang-analyzer-osx.cocoa.NSAutoreleasePool,clang-analyzer-osx.cocoa.NSError,clang-analyzer-osx.cocoa.NilArg,clang-analyzer-osx.cocoa.NonNilReturnValue,clang-analyzer-osx.cocoa.ObjCGenerics,clang-analyzer-osx.cocoa.RetainCount,clang-analyzer-osx.cocoa.RetainCountBase,clang-analyzer-osx.cocoa.RunLoopAutoreleaseLeak,clang-analyzer-osx.cocoa.SelfInit,clang-analyzer-osx.cocoa.SuperDealloc,clang-analyzer-osx.cocoa.UnusedIvars,clang-analyzer-osx.cocoa.VariadicMethodTypes,clang-analyzer-osx.coreFoundation.CFError,clang-analyzer-osx.coreFoundation.CFNumber,clang-analyzer-osx.coreFoundation.CFRetainRelease,clang-analyzer-osx.coreFoundation.containers.OutOfBounds,clang-analyzer-osx.coreFoundation.containers.PointerSizedValues,clang-analyzer-security.FloatLoopCounter,clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling,clang-analyzer-security.insecureAPI.SecuritySyntaxChecker,clang-analyzer-security.insecureAPI.UncheckedReturn,clang-analyzer-security.insecureAPI.bcmp,clang-analyzer-security.insecureAPI.bcopy,clang-analyzer-security.insecureAPI.bzero,clang-analyzer-security.insecureAPI.decodeValueOfObjCType,clang-analyzer-security.insecureAPI.getpw,clang-analyzer-security.insecureAPI.gets,clang-analyzer-security.insecureAPI.mkstemp,clang-analyzer-security.insecureAPI.mktemp,clang-analyzer-security.insecureAPI.rand,clang-analyzer-security.insecureAPI.strcpy,clang-analyzer-security.insecureAPI.vfork,clang-analyzer-unix.API,clang-analyzer-unix.DynamicMemoryModeling,clang-analyzer-unix.Malloc,clang-analyzer-unix.MallocSizeof,clang-analyzer-unix.MismatchedDeallocator,clang-analyzer-unix.Vfork,clang-analyzer-unix.cstring.BadSizeArg,clang-analyzer-unix.cstring.CStringModeling,clang-analyzer-unix.cstring.NullArg,clang-analyzer-valist.CopyToSelf,clang-analyzer-valist.Uninitialized,clang-analyzer-valist.Unterminated,clang-analyzer-valist.ValistBase,clang-analyzer-webkit.NoUncountedMemberChecker,clang-analyzer-webkit.RefCntblBaseVirtualDtor,clang-analyzer-webkit.UncountedLambdaCapturesChecker,concurrency-mt-unsafe,cppcoreguidelines-avoid-c-arrays,cppcoreguidelines-avoid-goto,cppcoreguidelines-avoid-magic-numbers,cppcoreguidelines-avoid-non-const-global-variables,cppcoreguidelines-c-copy-assignment-signature,cppcoreguidelines-explicit-virtual-functions,cppcoreguidelines-init-variables,cppcoreguidelines-interfaces-global-init,cppcoreguidelines-narrowing-conversions,cppcoreguidelines-no-malloc,cppcoreguidelines-non-private-member-variables-in-classes,cppcoreguidelines-owning-memory,cppcoreguidelines-pro-bounds-array-to-pointer-decay,cppcoreguidelines-pro-bounds-constant-array-index,cppcoreguidelines-pro-type-const-cast,cppcoreguidelines-pro-type-cstyle-cast,cppcoreguidelines-pro-type-member-init,cppcoreguidelines-pro-type-reinterpret-cast,cppcoreguidelines-pro-type-static-cast-downcast,cppcoreguidelines-pro-type-union-access,cppcoreguidelines-pro-type-vararg,cppcoreguidelines-slicing,cppcoreguidelines-special-member-functions,llvm-else-after-return,llvm-include-order,llvm-namespace-comment,llvm-prefer-isa-or-dyn-cast-in-conditionals,llvm-prefer-register-over-unsigned,llvm-qualified-auto,llvm-twine-local,misc-definitions-in-headers,misc-misplaced-const,misc-new-delete-overloads,misc-no-recursion,misc-non-copyable-objects,misc-non-private-member-variables-in-classes,misc-redundant-expression,misc-static-assert,misc-throw-by-value-catch-by-reference,misc-unconventional-assign-operator,misc-uniqueptr-reset-release,misc-unused-alias-decls,misc-unused-parameters,misc-unused-using-decls,modernize-avoid-bind,modernize-avoid-c-arrays,modernize-concat-nested-namespaces,modernize-deprecated-headers,modernize-deprecated-ios-base-aliases,modernize-loop-convert,modernize-make-shared,modernize-make-unique,modernize-pass-by-value,modernize-raw-string-literal,modernize-redundant-void-arg,modernize-replace-auto-ptr,modernize-replace-disallow-copy-and-assign-macro,modernize-replace-random-shuffle,modernize-return-braced-init-list,modernize-shrink-to-fit,modernize-unary-static-assert,modernize-use-auto,modernize-use-bool-literals,modernize-use-default-member-init,modernize-use-emplace,modernize-use-equals-default,modernize-use-equals-delete,modernize-use-noexcept,modernize-use-nullptr,modernize-use-override,modernize-use-transparent-functors,modernize-use-uncaught-exceptions,modernize-use-using,performance-faster-string-find,performance-for-range-copy,performance-implicit-conversion-in-loop,performance-inefficient-algorithm,performance-inefficient-string-concatenation,performance-inefficient-vector-operation,performance-move-const-arg,performance-move-constructor-init,performance-no-automatic-move,performance-no-int-to-ptr,performance-noexcept-move-constructor,performance-trivially-destructible,performance-type-promotion-in-math-fn,performance-unnecessary-copy-initialization,performance-unnecessary-value-param,portability-restrict-system-includes,portability-simd-intrinsics,readability-avoid-const-params-in-decls,readability-braces-around-statements,readability-const-return-type,readability-container-size-empty,readability-convert-member-functions-to-static,readability-delete-null-pointer,readability-deleted-default,readability-else-after-return,readability-function-cognitive-complexity,readability-function-size,readability-identifier-naming,readability-implicit-bool-conversion,readability-inconsistent-declaration-parameter-name,readability-isolate-declaration,readability-magic-numbers,readability-make-member-function-const,readability-misleading-indentation,readability-misplaced-array-index,readability-named-parameter,readability-non-const-parameter,readability-qualified-auto,readability-redundant-access-specifiers,readability-redundant-control-flow,readability-redundant-declaration,readability-redundant-function-ptr-dereference,readability-redundant-member-init,readability-redundant-preprocessor,readability-redundant-smartptr-get,readability-redundant-string-cstr,readability-redundant-string-init,readability-simplify-boolean-expr,readability-simplify-subscript-expr,readability-static-accessed-through-instance,readability-static-definition-in-anonymous-namespace,readability-string-compare,readability-uniqueptr-delete-release,readability-uppercase-literal-suffix,readability-use-anyofallof'
WarningsAsErrors: ''
HeaderFilterRegex: '.*'
FormatStyle: 'file'
User: rudy
...
41 changes: 41 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Contributing
## Built using
[Visual Studio Community 2022](https://visualstudio.microsoft.com/vs/) - IDE

[Clang Power Tools](https://marketplace.visualstudio.com/items?itemName=caphyon.ClangPowerTools) - VS2022 extension for Editor clang-format, clang-tidy

**Command-line usage of clang requires clang components while installing VS2022:**

![install](https://user-images.githubusercontent.com/61764259/199850257-8936d373-3792-4489-a666-a1e16d36a508.PNG)

***
**Without VS2022**
[LLVM](https://releases.llvm.org/) - Command-line clang-format and clang-tidy

## Formatting
Refer to [this](https://www.clangpowertools.com/blog/getting-started-with-clang-format-style-options.html) link for details.

To run the formatter in the command-line
```
clang-format -i ./src/*
```
***
To run the formatter in VS2022:

![format](https://user-images.githubusercontent.com/61764259/199850275-afdde603-dbdc-4a59-8fd7-612b68b2fa0a.png)

Click this button in the toolbar.

## Linter
Refer to [this](https://clang.llvm.org/extra/clang-tidy/) link for details.

To run the linter in the command-line:
```
clang-tidy --config="" ./src/*
```
***
To run the linter in VS2022:

![format](https://user-images.githubusercontent.com/61764259/199850275-afdde603-dbdc-4a59-8fd7-612b68b2fa0a.png)

Click this button in the toolbar.
6 changes: 6 additions & 0 deletions SauSaGe.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
<ClInclude Include="src\Files.h" />
<ClInclude Include="src\Text.h" />
</ItemGroup>
<ItemGroup>
<None Include=".clang-format" />
<None Include=".clang-tidy" />
<None Include="CONTRIBUTING.md" />
<None Include="README.md" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
Expand Down
41 changes: 18 additions & 23 deletions src/Files.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <fstream>
#include "Files.h"
#include <fstream>

Files::Files(std::string inputPathStr, std::string outputPathStr) {
Files::Files(const std::string& inputPathStr, const std::string& outputPathStr) {
inputPath = std::filesystem::path(inputPathStr);
outputPath = std::filesystem::path(outputPathStr);

Expand All @@ -10,17 +10,16 @@ Files::Files(std::string inputPathStr, std::string outputPathStr) {
// if filepath is a directory
if (std::filesystem::is_directory(inputPath)) {
// iterate through files in the directory recursively
for (const std::filesystem::directory_entry& i : std::filesystem::recursive_directory_iterator{ inputPath })
{
for (const std::filesystem::directory_entry& file : std::filesystem::recursive_directory_iterator{inputPath}) {
// if file is not a directory, add to texts vector
if (std::filesystem::is_directory(i.path()) && (i.path().extension().string() == ".txt" || i.path().extension().string() == ".md")) {
texts.push_back(i.path());
if (std::filesystem::is_directory(file.path()) &&
(file.path().extension().string() == ".txt" || file.path().extension().string() == ".md")) {
texts.emplace_back(file.path());
}
}
}
else {
} else {
// add single file to texts
texts.push_back(inputPath);
texts.emplace_back(inputPath);
}
}
}
Expand All @@ -29,14 +28,12 @@ void Files::createFiles() {
std::filesystem::remove_all(outputPath);
std::filesystem::create_directory(outputPath);
// create html files and move them to output directory
for (const Text& i : texts) {
// generate html
i.createHtml();
for (const Text& text : texts) {
// generate html
text.createHtml();
// move html file to output directory
std::filesystem::rename(
std::filesystem::path("./" + i.getHtmlName()).make_preferred(),
std::filesystem::path("./" + outputPath.string() + "/" + i.getHtmlName()).make_preferred()
);
std::filesystem::rename(std::filesystem::path("./" + text.getHtmlName()).make_preferred(),
std::filesystem::path("./" + outputPath.string() + "/" + text.getHtmlName()).make_preferred());
}

// if input path is a directory, generate an index page
Expand All @@ -48,7 +45,7 @@ void Files::createFiles() {
void Files::createIndexPage() {
std::ofstream ofs("index.html");
// index page opening tags
ofs << OPENTAGS[0] << "Index Page" << OPENTAGS[1] << "<ul>\n";
ofs << OPENTAGS1 << "Index Page" << OPENTAGS2 << "<ul>\n";

// links unordered list
for (const Text& i : texts) {
Expand All @@ -58,10 +55,8 @@ void Files::createIndexPage() {
// index page closing tags
ofs << "</ul>\n" << CLOSETAGS;

// close file stream and move index html file to output directory
// close file stream and move index html file to output directory
ofs.close();
std::filesystem::rename(
std::filesystem::path("./index.html").make_preferred(),
std::filesystem::path("./" + outputPath.string() + "/index.html").make_preferred()
);
}
std::filesystem::rename(std::filesystem::path("./index.html").make_preferred(),
std::filesystem::path("./" + outputPath.string() + "/index.html").make_preferred());
}
12 changes: 8 additions & 4 deletions src/Files.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#pragma once
#include <filesystem>
#ifndef FILES_H
#define FILES_H

#include "Text.h"
#include <filesystem>

class Files {
std::filesystem::path inputPath;
std::filesystem::path outputPath;
std::vector<Text> texts;
void createIndexPage();
public:
Files(std::string inputPath, std::string outputPath);

public:
Files(const std::string& inputPath, const std::string& outputPath);
void createFiles();
};
#endif
17 changes: 7 additions & 10 deletions src/Text.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <iostream>
#include <fstream>
#include "Text.h"
#include "maddy/parser.h"
#include <fstream>
#include <iostream>

Text::Text(std::filesystem::path filePath) {
Text::Text(const std::filesystem::path& filePath) {
m_filePath = filePath;
// get filename and extension from filepath
m_fileName = filePath.filename().string();
Expand Down Expand Up @@ -43,7 +43,7 @@ void Text::createHtml() const {
}

// add html and header tags
ofs << OPENTAGS[0] << title << OPENTAGS[1];
ofs << OPENTAGS1 << title << OPENTAGS2;

// add title to top of page
if (validTitle) {
Expand All @@ -59,15 +59,14 @@ void Text::createHtml() const {
if (m_fileExt == ".md") {
std::stringstream markdownInput(tempString);
ofs << parser->Parse(markdownInput);
}
else {
} else {
// if in paragraph output open paragraph tag, else output space (to account line break), then line
ofs << (inParagraph ? " " : "<p>") << tempString;
inParagraph = true;
}
}
// if line is blank, output close paragraph tag, no longer in paragraph
else if (inParagraph == true && tempString.length() <= 0) {
else if (inParagraph && tempString.length() <= 0) {
ofs << "</p>" << std::endl;
inParagraph = false;
}
Expand All @@ -77,6 +76,4 @@ void Text::createHtml() const {
ofs << CLOSETAGS;
}

std::string Text::getHtmlName() const {
return m_fileName.substr(0, m_fileName.rfind('.')) + ".html";
}
std::string Text::getHtmlName() const { return m_fileName.substr(0, m_fileName.rfind('.')) + ".html"; }
17 changes: 9 additions & 8 deletions src/Text.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
#pragma once
#ifndef TEXT_H
#define TEXT_H
#include <filesystem>

// open tags split into array to insert title
const std::string OPENTAGS[2] = {
"<!doctype html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<title>",
"</title>\n <meta name=\"viewport\" content=\"width=device-width,initial-scale=1\">\n</head>\n<body>\n"
};
const std::string CLOSETAGS = "</body>\n</html>\n";
const std::string OPENTAGS1 = "<!doctype html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<title>",
OPENTAGS2 = "</title>\n <meta name=\"viewport\" content=\"width=device-width,initial-scale=1\">\n</head>\n<body>\n",
CLOSETAGS = "</body>\n</html>\n";

class Text {
std::filesystem::path m_filePath;
std::string m_fileName;
std::string m_fileExt;
bool validTitle;
public:

public:
Text() = delete;
Text(std::filesystem::path filePath);
Text(const std::filesystem::path& filePath);
void createHtml() const;
std::string getHtmlName() const;
};
#endif
Loading

0 comments on commit 7471168

Please sign in to comment.