From 9a02a64258e36f05f2aa1b17bae8b83146a18001 Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Fri, 29 Nov 2019 14:07:56 +0330 Subject: update --- README.md | 1 + cgrep.cpp | 34 ++++++++++++++++++++++++++-------- compile_commands.json | 2 +- pch.hpp | 1 + 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 58463b6..6ae12cf 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ Coverity Scan Build Status +[![Coverage Status](https://coveralls.io/repos/github/bloodstalker/cgrep/badge.svg?branch=master)](https://coveralls.io/github/bloodstalker/cgrep?branch=master) # cgrep `grep` for C/C++ source files.
diff --git a/cgrep.cpp b/cgrep.cpp index 1d80ccb..fdf0323 100644 --- a/cgrep.cpp +++ b/cgrep.cpp @@ -19,11 +19,11 @@ using namespace clang::tooling; /*************************************************************************************************/ namespace { static llvm::cl::OptionCategory CGrepCat("cgrep options"); -cl::opt CO_DIRECTORY( +cl::opt CO_RECURSIVE( "dir", cl::desc("recursively goes through all the files and directories. assumes " - "compilation databases are present for all source files."), - cl::init(""), cl::cat(CGrepCat), cl::Optional); + "compilation databases are present for all source files."), + cl::init(""), cl::cat(CGrepCat), cl::Optional); // done cl::opt CO_REGEX("regex", cl::desc("the regex to match against"), cl::init(""), cl::cat(CGrepCat), cl::Required); // done @@ -119,6 +119,20 @@ cl::opt CO_B( #define NORMAL "\033[0m" #define CLEAR "\033[2J" +/** + * @brief recursively goes through all the directories starting from path + * + * @param path + */ +static void dig(std::string path) { + for (const auto &entry : std::filesystem::directory_iterator(path)) { + std::cout << entry.path() << "\n"; + if (true == entry.is_directory()) { + dig(entry.path()); + } + } +} + /** * @brief does some preprocessing on the regex string we get as input * @param rx_str @@ -172,8 +186,9 @@ void output_handler(const MatchFinder::MatchResult &MR, SourceRange SR, std::cout << line[i]; } } - if (isdecl) - std::cout << GREEN << "\t<---defined here" << NORMAL << "\n"; + if (isdecl) { + std::cout << GREEN << "\t<---declared here" << NORMAL << "\n"; + } } else { std::cout << line << "\n"; } @@ -228,10 +243,12 @@ public: return void(); std::string name = FD->getNameAsString(); if (regex_handler(REGEX_PP(CO_REGEX), name)) { + ast_type_traits::DynTypedNode DNode = ast_type_traits::DynTypedNode::create(*FD); + NamedDecl const * ND = DNode.get(); auto StartLocation = FD->getLocation(); auto EndLocation = StartLocation.getLocWithOffset(name.size() - 1); auto Range = SourceRange(StartLocation, EndLocation); - output_handler(MR, Range, *MR.SourceManager, true); + output_handler(MR, Range, *MR.SourceManager, FD->isThisDeclarationADefinition()); } } } @@ -247,8 +264,6 @@ public: virtual void run(const MatchFinder::MatchResult &MR) { const FieldDecl *FD = MR.Nodes.getNodeAs("fielddecl"); if (FD) { - // IdentifierInfo* ID = VD->getIdentifier(); - // SourceRange SR = VD->getSourceRange(); SourceRange SR = FD->getSourceRange(); SourceLocation SL = SR.getBegin(); CheckSLValidity(SL); @@ -769,6 +784,9 @@ int main(int argc, const char **argv) { op.getSourcePathList(); ClangTool Tool(op.getCompilations(), op.getSourcePathList()); int ret = Tool.run(newFrontendActionFactory().get()); + if ("" != CO_RECURSIVE) { + dig(CO_RECURSIVE); + } return ret; } /*************************************************************************************************/ diff --git a/compile_commands.json b/compile_commands.json index fcd399f..2803ad9 100644 --- a/compile_commands.json +++ b/compile_commands.json @@ -1,6 +1,6 @@ [ { - "command": "c++ -c -include-pch pch.hpp.gch -fpic -I/home/bloodstalker/extra/llvm-clang-4/llvm/include -I/home/bloodstalker/extra/llvm-clang-4/build/include -std=c++11 -fno-exceptions -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/bloodstalker/extra/llvm-clang-4/llvm/tools/clang/include -I/home/bloodstalker/extra/llvm-clang-4/build/tools/clang/include -std=c++17 -fexceptions -o cgrep.o cgrep.cpp", + "command": "c++ -c -include-pch pch.hpp.gch -fpic -I/home/bloodstalker/extra/llvm-clang-4/llvm/include -I/home/bloodstalker/extra/llvm-clang-4/build/include -std=c++14 -fno-exceptions -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/bloodstalker/extra/llvm-clang-4/llvm/tools/clang/include -I/home/bloodstalker/extra/llvm-clang-4/build/tools/clang/include -std=c++17 -fexceptions -o cgrep.o cgrep.cpp", "directory": "/home/bloodstalker/extra/cgrep", "file": "/home/bloodstalker/extra/cgrep/cgrep.cpp" } diff --git a/pch.hpp b/pch.hpp index ec1da21..e54a573 100644 --- a/pch.hpp +++ b/pch.hpp @@ -18,3 +18,4 @@ #include #include #include +#include -- cgit v1.2.3