From c8ecbde8b5b90aa6c4d6ed0cb29b460d214b8ce4 Mon Sep 17 00:00:00 2001
From: "Xuefeng Ding (IHEP)" <dingxf@ihep.ac.cn>
Date: Thu, 14 Nov 2024 04:23:26 +0800
Subject: [PATCH] update

---
 .clang-format                                 | 17 +++++
 .clang-tidy                                   | 62 +++++++++++++++++
 .vscode/settings.json                         | 17 ++++-
 README.md                                     | 68 ++++++++++++++++++-
 cpp/CMakeLists.txt                            | 10 +--
 cpp/include/complex.h                         |  3 -
 cpp/include/complexf.h                        |  3 +
 cpp/src/complex.cc                            |  4 --
 cpp/src/complexf.cc                           |  5 ++
 noxfile.py                                    | 15 ++++
 python/myproj/simulation/_csrc/CMakeLists.txt |  2 +-
 .../myproj/simulation/_csrc/_hello_python.cc  |  9 ++-
 12 files changed, 194 insertions(+), 21 deletions(-)
 create mode 100644 .clang-format
 create mode 100644 .clang-tidy
 delete mode 100644 cpp/include/complex.h
 create mode 100644 cpp/include/complexf.h
 delete mode 100644 cpp/src/complex.cc
 create mode 100644 cpp/src/complexf.cc

diff --git a/.clang-format b/.clang-format
new file mode 100644
index 0000000..f608358
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,17 @@
+---
+Language: Cpp
+BasedOnStyle: Google
+ColumnLimit: 120
+NamespaceIndentation: All
+SortIncludes: true
+IndentWidth: 2
+AccessModifierOffset: -2
+PenaltyBreakComment: 30
+PenaltyExcessCharacter: 100
+AlignAfterOpenBracket: Align
+AllowShortIfStatementsOnASingleLine: false
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakTemplateDeclarations: Yes
+ReflowComments: false
+BinPackArguments: false
+BinPackParameters: false
diff --git a/.clang-tidy b/.clang-tidy
new file mode 100644
index 0000000..901f98a
--- /dev/null
+++ b/.clang-tidy
@@ -0,0 +1,62 @@
+Checks: >-
+  bugprone-*,
+  clang-analyzer-*,
+  concurrency-*,
+  cppcoreguidelines-*,
+  google-*,
+  hicpp-*,
+  llvm-*,
+  misc-*,
+  modernize-*,
+  performance-*,
+  portability-*,
+  readability-*,
+  -modernize-use-trailing-return-type,
+  -readability-identifier-length,
+  -readability-magic-numbers,
+  -cppcoreguidelines-avoid-magic-numbers,
+  -bugprone-easily-swappable-parameters,
+  -misc-include-cleaner,
+  -llvm-header-guard,
+  -modernize-macro-to-enum,
+  -bugprone-use-after-move,
+  -hicpp-invalid-access-moved,
+  -misc-const-correctness
+
+CheckOptions:
+  - { key: readability-identifier-naming.NamespaceCase, value: lower_case }
+  - { key: readability-identifier-naming.ClassCase, value: CamelCase }
+  - { key: readability-identifier-naming.StructCase, value: CamelCase }
+  - {
+      key: readability-identifier-naming.TemplateParameterCase,
+      value: aNy_CasE,
+    }
+  - { key: readability-identifier-naming.FunctionCase, value: aNy_CasE }
+  - { key: readability-identifier-naming.VariableCase, value: lower_case }
+  - { key: readability-identifier-naming.ClassMemberCase, value: lower_case }
+  - { key: readability-identifier-naming.ClassMemberPrefix, value: f_ }
+  - { key: readability-identifier-naming.MethodCase, value: CamelCase }
+  - { key: readability-identifier-naming.PrivateMemberPrefix, value: m_ }
+  - { key: readability-identifier-naming.ProtectedMemberPrefix, value: m_ }
+  - { key: readability-identifier-naming.EnumConstantCase, value: camelBack }
+  - {
+      key: readability-identifier-naming.ConstexprVariableCase,
+      value: CamelCase,
+    }
+  - { key: readability-identifier-naming.ConstexprVariablePrefix, value: k }
+  - { key: readability-identifier-naming.GlobalConstantCase, value: CamelCase }
+  - { key: readability-identifier-naming.GlobalConstantPrefix, value: f }
+  - { key: readability-identifier-naming.MemberConstantCase, value: CamelCase }
+  - { key: readability-identifier-naming.MemberConstantPrefix, value: k }
+  - { key: readability-identifier-naming.StaticConstantCase, value: CamelCase }
+  - { key: readability-identifier-naming.StaticConstantPrefix, value: k }
+  - {
+      key: misc-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic,
+      value: 1,
+    }
+  - { key: readability-function-cognitive-complexity.Threshold, value: 21 }
+
+WarningsAsErrors: "*"
+HeaderFilterRegex: ""
+FormatStyle: file
+User: ""
diff --git a/.vscode/settings.json b/.vscode/settings.json
index 97453c1..0ce852d 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -10,5 +10,20 @@
   },
   "editor.formatOnSave": true,
   "isort.args": ["--profile", "black"],
-  "notebook.lineNumbers": "on"
+  "notebook.lineNumbers": "on",
+  "clangd.arguments": [
+    "--log=info",
+    "--pretty",
+    "--all-scopes-completion",
+    "--completion-style=bundled",
+    "--header-insertion=iwyu",
+    "--header-insertion-decorators",
+    "--background-index",
+    "--clang-tidy",
+    "-j=20",
+    "--pch-storage=disk",
+    "--function-arg-placeholders=false",
+    "--compile-commands-dir=build"
+  ],
+  "clangd.path": "clangd-17"
 }
diff --git a/README.md b/README.md
index ad6f472..976a320 100644
--- a/README.md
+++ b/README.md
@@ -139,10 +139,74 @@ repos:
 ```
 ### c++ lint
 - nox + vscode extensions
+- setup nox
+```python
+@nox.session(venv_backend="none")
+def dev(session: nox.Session) -> None:
+    """
+    Prepare a .venv folder.
+    """
+
+    session.run(sys.executable, "-m", "venv", ".venv")
+    session.run(
+        ".venv/bin/pip",
+        "install",
+        "scikit-build-core[pyproject]",
+        "pybind11",
+        "pip>=23",
+    )
+    session.run(
+        ".venv/bin/pip",
+        "install",
+        "--no-build-isolation",
+        "--check-build-dependencies",
+        "-ve.",
+        "-C",
+        "cmake.define.CMAKE_EXPORT_COMPILE_COMMANDS=1",
+        "-C",
+        "build-dir=build",
+    )
+
+@nox.session(reuse_venv=True)
+def cpp_lint(session: nox.Session) -> None:
+    cpp_files = glob.glob("**/*.cc", recursive=True)
+    session.install("clangd-tidy")
+    session.run("clangd-tidy", "-p", "build", *cpp_files)
+```
+- vscode extensions. use `clangd`
+```
+Name: clangd
+Id: llvm-vs-code-extensions.vscode-clangd
+Description: C/C++ completion, navigation, and insights
+Version: 0.1.30
+Publisher: LLVM
+VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd
+```
+and set
+```json
+"clangd.arguments": [
+    "--log=info",
+    "--pretty",
+    "--all-scopes-completion",
+    "--completion-style=bundled",
+    "--header-insertion=iwyu",
+    "--header-insertion-decorators",
+    "--background-index",
+    "--clang-tidy",
+    "-j=20",
+    "--pch-storage=disk",
+    "--function-arg-placeholders=false",
+    "--compile-commands-dir=build"
+  ],
+"clangd.path": "clangd-17"
+```
 
 ### python lint
-- ruff: pre-commit + vscode extensions
-- pylint: nox + vscode extensions
+#### ruff: pre-commit + vscode extensions
+- install pre-commit
+
+- install extensions
+#### pylint: nox + vscode extensions
 
 ## vscode extensions to install
 - "isort", which sort python import
diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt
index 2dedfcf..18e508b 100644
--- a/cpp/CMakeLists.txt
+++ b/cpp/CMakeLists.txt
@@ -4,13 +4,13 @@ target_include_directories(hello PUBLIC
     $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
 install(TARGETS hello LIBRARY DESTINATION .)
 
-add_library(complex SHARED src/complex.cc)
-target_include_directories(complex PUBLIC
+add_library(complexf SHARED src/complexf.cc)
+target_include_directories(complexf PUBLIC
     $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
     $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
-target_link_libraries(complex PRIVATE hello)
-install(TARGETS complex LIBRARY DESTINATION .)
-set_target_properties(complex PROPERTIES
+target_link_libraries(complexf PRIVATE hello)
+install(TARGETS complexf LIBRARY DESTINATION .)
+set_target_properties(complexf PROPERTIES
     INSTALL_RPATH "$ORIGIN"
 )
 
diff --git a/cpp/include/complex.h b/cpp/include/complex.h
deleted file mode 100644
index 0ebcf62..0000000
--- a/cpp/include/complex.h
+++ /dev/null
@@ -1,3 +0,0 @@
-#pragma once
-
-double complex(double a, double b);
diff --git a/cpp/include/complexf.h b/cpp/include/complexf.h
new file mode 100644
index 0000000..4a6f20f
--- /dev/null
+++ b/cpp/include/complexf.h
@@ -0,0 +1,3 @@
+#pragma once
+
+double complexf(double a, double b);
diff --git a/cpp/src/complex.cc b/cpp/src/complex.cc
deleted file mode 100644
index 3114e15..0000000
--- a/cpp/src/complex.cc
+++ /dev/null
@@ -1,4 +0,0 @@
-#include "complex.h"
-#include "hello.h"
-
-double complex(double a, double b) { return add(a, b) + 2; }
diff --git a/cpp/src/complexf.cc b/cpp/src/complexf.cc
new file mode 100644
index 0000000..0232427
--- /dev/null
+++ b/cpp/src/complexf.cc
@@ -0,0 +1,5 @@
+#include "complexf.h"
+
+#include "hello.h"
+
+double complexf(double a, double b) { return add(a, b) + 2; }
diff --git a/noxfile.py b/noxfile.py
index a611171..f877b03 100644
--- a/noxfile.py
+++ b/noxfile.py
@@ -2,6 +2,7 @@
 nox configuration file.
 """
 
+import glob
 import sys
 from pathlib import Path
 
@@ -46,6 +47,20 @@ def dev(session: nox.Session) -> None:
     )
 
 
+@nox.session(reuse_venv=True)
+def cpp_lint_slow(session: nox.Session) -> None:
+    if "cpp_lint_slow" in sys.argv:
+        cpp_files = glob.glob("**/*.cc", recursive=True)
+        session.run("clang-tidy", "-p", "build", *cpp_files)
+
+
+@nox.session(reuse_venv=True)
+def cpp_lint(session: nox.Session) -> None:
+    cpp_files = glob.glob("**/*.cc", recursive=True)
+    session.install("clangd-tidy")
+    session.run("clangd-tidy", "-p", "build", *cpp_files)
+
+
 @nox.session(reuse_venv=True)
 def rr_run(session: nox.Session) -> None:
     """
diff --git a/python/myproj/simulation/_csrc/CMakeLists.txt b/python/myproj/simulation/_csrc/CMakeLists.txt
index 3103c4a..796e35d 100644
--- a/python/myproj/simulation/_csrc/CMakeLists.txt
+++ b/python/myproj/simulation/_csrc/CMakeLists.txt
@@ -2,7 +2,7 @@ find_package(Python COMPONENTS Interpreter Development.Module REQUIRED)
 find_package(pybind11 CONFIG REQUIRED)
 
 pybind11_add_module(_hello MODULE _hello_python.cc)
-target_link_libraries(_hello PRIVATE hello complex)
+target_link_libraries(_hello PRIVATE complexf)
 install(TARGETS _hello DESTINATION _csrc)
 set_target_properties(_hello PROPERTIES
     INSTALL_RPATH "$ORIGIN/.."
diff --git a/python/myproj/simulation/_csrc/_hello_python.cc b/python/myproj/simulation/_csrc/_hello_python.cc
index 21078d9..594169f 100644
--- a/python/myproj/simulation/_csrc/_hello_python.cc
+++ b/python/myproj/simulation/_csrc/_hello_python.cc
@@ -1,12 +1,11 @@
-#include "complex.h"
-#include "hello.h"
 #include <pybind11/pybind11.h>
 
-namespace py = pybind11;
+#include "complexf.h"
+#include "hello.h"
 
 PYBIND11_MODULE(_hello, m) {
-  m.doc() = "pybind11 example plugin"; // optional module docstring
+  m.doc() = "pybind11 example plugin";  // optional module docstring
 
   m.def("add", &add, "A function which adds two numbers");
-  m.def("complex", &complex, "complex(a,b) = add(a,b) + 2");
+  m.def("complex", &complexf, "complex(a,b) = add(a,b) + 2");
 }
-- 
GitLab