GithubHelp home page GithubHelp logo

cncc's Introduction

Customizable Naming Convention Checker

CNCC is a customizable naming conventions checker for C++. In using the Clang frontend CNCC works directly on the language's abstract syntax tree (AST). This is quite similar to what clang-format does but way more simple and for naming conventions only.

In short, CNCC supports user-defined pattern validation based on AST nodes: you can specify patterns for variables, class names, namespace names, and so on.

Note: recent this patch introduces the concept to clang-tidy.

Example

You provide a style file containing regular expressions for validating AST nodes, such as: camel-case for class names, members should have an underscore appended, and so on.

class_decl: '^([A-Z][a-z]+)+$'
field_decl: '^[a-z]+_$'
var_decl: '^[a-z][a-z0-9]*$'
namespace: '^[a-z]?$'

CNCC then checks the AST's nodes against the provided rules, issuing warnings to stderr and returning with a appropriate status code for easy integration:

test.cc:4:11: "myNamespace" does not conform to pattern "^[a-z]?$" associated with namespace
test.cc:6:7: "myClass" does not conform to pattern "^([A-Z][a-z]+)+$" associated with class_decl
test.cc:11:9: "myMember" does not conform to pattern "^[a-z]+_$" associated with field_decl
test.cc:19:24: "myMember" does not conform to pattern "^[a-z]+_$" associated with field_decl

For all available nodes see the official libclang documentation or the Python wrapper documentation and look for CursorKind.

Requirements

  • python2
  • python-clang
  • python-yaml

Usage

For standalone files, with default compiler invocation:

cncc --style=examples/small.style examples/test.cc

CNCC defaults to reading a .cncc.style file in your home directory if no style file was given.

cncc examples/test.cc

Sometimes compilation gets more involved. Use CMake or Bear in order to create a compile_command.json compilation database. In it compiler invocation flags and arguments are stored for each file involved in the build process. This does not require you to build your program.

mkdir build
cd build
ccmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=1
cncc --style=~/.cncc.style --dbdir . ../main.cc

License

Copyright © 2015 Daniel J. Hofmann

Distributed under the MIT License (MIT).

cncc's People

Contributors

daniel-j-h avatar themarex avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cncc's Issues

Implement test cases

Test directory with test case C++ programs and style files to validate against.

Is this project dead?

Hi,

This looks awesome and just like something I could use perfectly and built upon. Do you guys support it and accept PRs and stuff?

Cheers

Examples for CursorKind

Examples directory for various / all CursorKinds and how the kinds map to C++ classes, variables, and so on.

Maybe provide a single big C++ file and annotate it based on the AST.

AttributeError: 'CompileCommand' object has no attribute 'encode'

Hi I found this project while looking for a regex-based check to clang-tidy for checking a highly non-standard naming convention and I thought I'd give it a go.

I installed:
python 3.8 (win python distribution - i'm running windows)
clang 11.0 (https://pypi.org/project/clang/)
pyyaml 5.3.1 (https://pypi.org/project/PyYAML/)

I have clang 10.0.0 installed aswell and added the libclang.dll to my PATH. Prior to running cncc I also generated a compiler database.

When I run python.exe cncc --style examples\basic.style --dbdir d:\path\to\folder\with\compile\database d:\some\file.c d:\some\other\file.c I get the following error:

Traceback (most recent call last):
File "cncc", line 44, in
unit = index.parse(f, args=commands)
File "C:\Users\username\Desktop\WPy64-3850\python-3.8.5.amd64\lib\site-packages\clang\cindex.py", line 2721, in parse
return TranslationUnit.from_source(path, args, unsaved_files, options,
File "C:\Users\username\Desktop\WPy64-3850\python-3.8.5.amd64\lib\site-packages\clang\cindex.py", line 2816, in from_source
args_array = (c_char_p * len(args))([b(x) for x in args])
File "C:\Users\username\Desktop\WPy64-3850\python-3.8.5.amd64\lib\site-packages\clang\cindex.py", line 2816, in
args_array = (c_char_p * len(args))(
[b(x) for x in args])
File "C:\Users\username\Desktop\WPy64-3850\python-3.8.5.amd64\lib\site-packages\clang\cindex.py", line 109, in b
return x.encode('utf8')
AttributeError: 'CompileCommand' object has no attribute 'encode'

I was hoping maybe you could shed a little light on this? The compilation database works fine when using just clang-tidy, so I don't suppose it's corrupt in any way.

Error when running: "libclang.so: cannot open shared object file: No such file or directory"

First, I ran sudo apt-get install python-yaml and sudo pip install clang. Success.

Then I tried:

[dev@ubuntu:~/cncc (master)] $ ls
cncc  examples  LICENSE  MyClass.cpp  MyClass.h  README.md  util
[dev@ubuntu:~/cncc (master)] $ git log -1
commit 4529cb3536c7cec20ea0bb850d0f95e80cded733
Author: Daniel J. Hofmann <[email protected]>
Date:   Wed Aug 19 14:55:48 2015 +0200

    Respect global style file, closes #5

    Local style files do not seem to make a lot sense. What we could do is
    walk all parent directories like `clang-format` does, though.
[dev@ubuntu:~/cncc (master)] $ cat MyClass.h 
class MyClass{
public:
    void init(int a, int b);
    int loopAlot();
private:
    int var1, var2;
};

[dev@ubuntu:~/cncc (master)] $ cat MyClass.cpp 
#include "MyClass.h"

void MyClass::init(int a, int b){
    var1 = a;
    var2 = b;
}

int MyClass::loopAlot(){
    int res = this->var1 + var2;
    for(int i=0; i<this->var1; ++i){
        res = res + this->var2;
    }
    return res;
}
[dev@ubuntu:~/cncc (master)] $ ./cncc --style=examples/basic.style MyClass.cpp
Traceback (most recent call last):
  File "./cncc", line 22, in <module>
    index = I.create()
  File "/usr/local/lib/python2.7/dist-packages/clang/cindex.py", line 2119, in create
    return Index(conf.lib.clang_createIndex(excludeDecls, 0))
  File "/usr/local/lib/python2.7/dist-packages/clang/cindex.py", line 141, in __get__
    value = self.wrapped(instance)
  File "/usr/local/lib/python2.7/dist-packages/clang/cindex.py", line 3429, in lib
    lib = self.get_cindex_library()
  File "/usr/local/lib/python2.7/dist-packages/clang/cindex.py", line 3460, in get_cindex_library
    raise LibclangError(msg)
clang.cindex.LibclangError: libclang.so: cannot open shared object file: No such file or directory. To provide a path to libclang use Config.set_library_path() or Config.set_library_file().

Any idea what I'm doing wrong?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.