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

19 header file do not check fully #36

Merged
merged 4 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion script/build-pack-win32.cmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@ECHO OFF

IF EXIST release (RD release)
IF EXIST release (RD /S /Q release)
python cppnamelint.py bldgpack .. .\release

ECHO.
Expand Down
2 changes: 1 addition & 1 deletion script/cppnamelint.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def convert_py_args_to_exe_args(py_args) -> str:
def find_sample_files(start_dir: str) -> []:
paired_list = []

include_src_files: [] = ['.c', '.cpp']
include_src_files: [] = ['.c', '.cpp', '.h']
include_cfg_files: [] = ['.toml']

file_obj = File()
Expand Down
336 changes: 109 additions & 227 deletions source/LearnIt.cpp

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions source/LearnIt.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class LearnIt {
void _PrintFunctionDecl(FunctionDecl *pDecl);
void _PrintParmVarDecl(ParmVarDecl *pDecl);
void _PrintRecordDecl(RecordDecl *pDecl);
void _PrintTypedefDecl(TypedefDecl *pDecl);
};

#endif // __LEARN_IT_H__
70 changes: 44 additions & 26 deletions source/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,53 +28,64 @@ bool WriteJsonResult(const MemoBoard &MemoBoard, const string &FilePath);

int RunCheck(namelint::MemoBoard &Memo) {
int iRet = 0;
string InputFile = CheckInputSrc;
string InpuConfig = CheckInputConfig;

string OutputJson = CheckOutputJson;
vector<string> InputIncs;

DcLib::Log::Out(INFO_ALL, "InputFile = %s", InputFile.c_str());
DcLib::Log::Out(INFO_ALL, "InpuConfig = %s", InpuConfig.c_str());
DcLib::Log::Out(INFO_ALL, "OutputJson = %s", OutputJson.c_str());
Memo.File.Source = CheckInputSrc;
Memo.File.Config = CheckInputConfig;
Memo.Dir.Includes = InputIncs;

if (OutputJson.length() == 0) {
OutputJson = "cppnamelint.json";
}

if (!llvm::sys::fs::exists(InputFile)) {
//
// Check input parameters.
//
DcLib::Log::Out(INFO_ALL, "Source File = %s", Memo.File.Source.c_str());
DcLib::Log::Out(INFO_ALL, "Config File = %s", Memo.File.Config.c_str());
DcLib::Log::Out(INFO_ALL, "OutputJson = %s", OutputJson.c_str());

if (!llvm::sys::fs::exists(Memo.File.Source)) {
cout << "Error: Failed to find input source file." << endl;
return 1;
}

if (!llvm::sys::fs::exists(InpuConfig)) {
if (!llvm::sys::fs::exists(Memo.File.Config)) {
cout << "Error: Failed to find config file." << endl;
return 2;
}

string errorReason;
if (!Memo.Config.LoadFile(InpuConfig, errorReason)) {
if (!Memo.Config.LoadFile(Memo.File.Config, errorReason)) {
cout << "Error: Failed to load config file (format wrong)." << endl;
cout << errorReason << endl;
return 3;
}

if (OutputJson.length() == 0) {
OutputJson = "cppnamelint.json";
}

//
// Load source code file then create compilatation database.
//
std::string ErrorMessage;
std::unique_ptr<CompilationDatabase> Compilations =
FixedCompilationDatabase::loadFromFile(InputFile, ErrorMessage);
auto Compilations =
FixedCompilationDatabase::loadFromFile(Memo.File.Source, ErrorMessage);

Memo.File.Source = InputFile;
Memo.File.Config = InpuConfig;
Memo.Dir.Includes = InputIncs;

vector<string> SingleFileInList = {InputFile};
//
// Create clang tool then add clang tool arguments.
//
vector<string> SingleFileInList = {Memo.File.Source};
ClangTool Tool(*Compilations, SingleFileInList);
// Tool.appendArgumentsAdjuster(getInsertArgumentAdjuster("--I./",
// ArgumentInsertPosition::BEGIN));
// Tool.appendArgumentsAdjuster(
// getInsertArgumentAdjuster("-v", ArgumentInsertPosition::BEGIN));
Tool.appendArgumentsAdjuster(getInsertArgumentAdjuster(
"--language=c++",
ArgumentInsertPosition::BEGIN)); // Make it parses header file.

Tool.setDiagnosticConsumer(new IgnoringDiagConsumer());

MyFactory MyFactory;
std::unique_ptr<FrontendActionFactory> Factory =
newFrontendActionFactory(&MyFactory);

Detection Detect;
shared_ptr<ConfigData> pConfig = Memo.Config.GetData();
GeneralOptions *pOptions = &pConfig->General.Options;
Expand All @@ -85,14 +96,19 @@ int RunCheck(namelint::MemoBoard &Memo) {

Memo.Checked.nFile++;

string FileBaseName = Path::FindFileName(InputFile);
string FileBaseName = Path::FindFileName(Memo.File.Source);
GeneralRules *pRules = &pConfig->General.Rules;
if (!Detect.CheckFile(pRules->FileName, FileBaseName)) {
Memo.Error.nFile++;
Memo.ErrorDetailList.push_back(new ErrorDetail(FileBaseName, ""));
}
}

MyFactory MyFactory;
std::unique_ptr<FrontendActionFactory> Factory =
newFrontendActionFactory(&MyFactory);

// Go
if (0 == Tool.run(Factory.get())) {
iRet = GetTotalError(Memo);
PrintTraceMemo(Memo);
Expand Down Expand Up @@ -193,8 +209,10 @@ bool PrintTraceMemo(const MemoBoard &MemoBoard) {
bool bStatus = true;
char szText[512] = {0};

cout << " File = " << MemoBoard.File.Source << endl;
cout << " Config = " << MemoBoard.File.Config << endl;
cout << " File = "
<< llvm::sys::path::filename(MemoBoard.File.Source).data() << endl;
cout << " Config = "
<< llvm::sys::path::filename(MemoBoard.File.Config).data() << endl;
for (size_t nIdx = 0; nIdx < MemoBoard.Dir.Includes.size(); nIdx++) {
sprintf(szText, " Inc[%2d] = %s", nIdx + 1,
MemoBoard.Dir.Includes[nIdx].c_str());
Expand Down
9 changes: 9 additions & 0 deletions source/MyAstVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,12 @@ bool MyASTVisitor::VisitParmVarDecl(ParmVarDecl *pDecl) {

return true;
}

bool MyASTVisitor::VisitTypedefDecl(TypedefDecl *pDecl) {
APP_CONTEXT *pAppCxt = ((APP_CONTEXT *)GetAppCxt());

if (pAppCxt->MemoBoard.Option.bEnableLog) {
this->m_LearnIt.PrintDecl(pDecl);
}
return true;
}
70 changes: 70 additions & 0 deletions source/MyAstVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,76 @@ class MyASTVisitor : public RecursiveASTVisitor<MyASTVisitor> {
bool VisitFieldDecl(FieldDecl *pDecl);
bool VisitReturnStmt(ReturnStmt *pRetStmt);
bool VisitParmVarDecl(ParmVarDecl *pDecl);
bool VisitTypedefDecl(TypedefDecl *pDecl);
};

/*
// Declaration visitors
bool VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
bool VisitTypeAliasDecl(TypeAliasDecl *D);
bool VisitAttributes(Decl *D);
bool VisitBlockDecl(BlockDecl *B);
bool VisitCXXRecordDecl(CXXRecordDecl *D);
Optional<bool> shouldVisitCursor(CXCursor C);
bool VisitDeclContext(DeclContext *DC);
bool VisitTranslationUnitDecl(TranslationUnitDecl *D);
bool VisitTypedefDecl(TypedefDecl *D);
bool VisitTagDecl(TagDecl *D);
bool VisitClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl *D);
bool VisitClassTemplatePartialSpecializationDecl(
ClassTemplatePartialSpecializationDecl *D);
bool VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
bool VisitEnumConstantDecl(EnumConstantDecl *D);
bool VisitDeclaratorDecl(DeclaratorDecl *DD);
bool VisitFunctionDecl(FunctionDecl *ND);
bool VisitFieldDecl(FieldDecl *D);
bool VisitVarDecl(VarDecl *);
bool VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
bool VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
bool VisitClassTemplateDecl(ClassTemplateDecl *D);
bool VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
bool VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
bool VisitObjCMethodDecl(ObjCMethodDecl *ND);
bool VisitObjCContainerDecl(ObjCContainerDecl *D);
bool VisitObjCCategoryDecl(ObjCCategoryDecl *ND);
bool VisitObjCProtocolDecl(ObjCProtocolDecl *PID);
bool VisitObjCPropertyDecl(ObjCPropertyDecl *PD);
bool VisitObjCTypeParamList(ObjCTypeParamList *typeParamList);
bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
bool VisitObjCImplDecl(ObjCImplDecl *D);
bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
bool VisitObjCImplementationDecl(ObjCImplementationDecl *D);
// FIXME: ObjCCompatibleAliasDecl requires aliased-class locations.
bool VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD);
bool VisitLinkageSpecDecl(LinkageSpecDecl *D);
bool VisitNamespaceDecl(NamespaceDecl *D);
bool VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
bool VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
bool VisitUsingDecl(UsingDecl *D);
bool VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
bool VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
bool VisitStaticAssertDecl(StaticAssertDecl *D);
bool VisitFriendDecl(FriendDecl *D);

// Name visitor
bool VisitDeclarationNameInfo(DeclarationNameInfo Name);
bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS, SourceRange Range);
bool VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);

// Template visitors
bool VisitTemplateParameters(const TemplateParameterList *Params);
bool VisitTemplateName(TemplateName Name, SourceLocation Loc);
bool VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL);

// Type visitors
#define ABSTRACT_TYPELOC(CLASS, PARENT)
#define TYPELOC(CLASS, PARENT) \
bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
#include "clang/AST/TypeLocNodes.def"

bool VisitTagTypeLoc(TagTypeLoc TL);
bool VisitArrayTypeLoc(ArrayTypeLoc TL);
bool VisitFunctionTypeLoc(FunctionTypeLoc TL, bool SkipResultType = false);
*/

#endif // __NAMELINT_MY_AST_VISITOR__H__
2 changes: 1 addition & 1 deletion source/test/sample/Sample01UpperCamel.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// [General.Rules]
// [General.Rules]
// FileName = 1 # 1: UpperCamel
// FunctionName = 1 # 1: UpperCamel
// VariableName = 1 # 1: UpperCamel
Expand Down
2 changes: 1 addition & 1 deletion source/test/sample/Sample01UpperCamel.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[General.Options]
[General.Options]
Version = 0.2
FileExtNameList = ["*.c","*.h","*.cpp"]
CheckVariableName = true
Expand Down
2 changes: 1 addition & 1 deletion source/test/sample/Sample02_UpperCamel.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// [General.Rules]
// [General.Rules]
// FileName = 1 # 1: UpperCamel
// FunctionName = 1 # 1: UpperCamel
// VariableName = 1 # 1: UpperCamel
Expand Down
2 changes: 1 addition & 1 deletion source/test/sample/Sample02_UpperCamel.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[General.Options]
[General.Options]
Version = 0.2
FileExtNameList = ["*.c","*.h","*.cpp"]
CheckVariableName = true
Expand Down
2 changes: 1 addition & 1 deletion source/test/sample/Sample06LowerCamel.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// [General.Rules]
// [General.Rules]
// FileName = 1 # 1: UpperCamel
// FunctionName = 1 # 1: UpperCamel
// variableName = 4 # 4: Hungarian
Expand Down
20 changes: 20 additions & 0 deletions source/test/sample/Sample07.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "Sample07.h"
#include "Sample07a.h"

#define min2(X, Y) ((X) < (Y) ? (X) : (Y))
typedef void (*fnFunc)(short s, short);

MyClass::MyClass() {}

MyClass::~MyClass() {}

bool MyClass::MyPubFunc(int) {
ITEM Item;
ITEM *pItem;
return true;
}

bool MyClass::MyPrivFunc(int Value) {
Value = min2(Value, 10);
return true;
}
9 changes: 9 additions & 0 deletions source/test/sample/Sample07.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class MyClass {
public:
MyClass();
~MyClass();
bool MyPubFunc(int);

private:
bool MyPrivFunc(int);
};
Loading