├── .github └── workflows │ └── cmake.yml ├── .gitignore ├── .gitmodules ├── .gitpod.Dockerfile ├── .gitpod.yml ├── .travis.yml ├── CMakeLists.txt ├── CONTRIBUTORS.md ├── Dockerfile ├── LICENSE ├── README.md ├── appveyor.yml ├── cgrep.cpp ├── cgrep.roff ├── covrun.sh ├── docker ├── 11 │ └── Dockerfile ├── 12 │ └── Dockerfile ├── 13 │ └── Dockerfile ├── 14 │ └── Dockerfile ├── 15 │ └── Dockerfile ├── 16 │ └── Dockerfile ├── 17 │ └── Dockerfile ├── 18 │ └── Dockerfile └── arch │ └── Dockerfile ├── makefile ├── makeman.sh ├── pch.hpp ├── run.sh ├── test ├── callexpr.cpp ├── classdecl.cpp ├── compile_commands.json ├── cxxmembercallexpr.cpp ├── cxxmethoddecl.cpp ├── cxxrecorddecl.cpp ├── declrefexpr.cpp ├── fielddecl.cpp ├── function.cpp ├── main.ast ├── main.cpp ├── makefile ├── nameddecldef.cpp ├── structdecl.cpp ├── test_list.md ├── uniondecdef.cpp └── vardecl.cpp └── testscript └── main.py /.github/workflows/cmake.yml: -------------------------------------------------------------------------------- 1 | name: CMake 2 | on: 3 | push: 4 | branches: [ "master" ] 5 | pull_request: 6 | branches: [ "master" ] 7 | env: 8 | BUILD_TYPE: Release 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | strategy: 13 | matrix: 14 | version: [15 ,16 ,17 ,18] 15 | steps: 16 | - uses: actions/checkout@v3 17 | - name: Get LLVM 18 | run: sudo apt install -y wget cmake git lsb-release software-properties-common gpg && wget https://apt.llvm.org/llvm.sh && chmod +x llvm.sh && sudo ./llvm.sh ${{matrix.version}} && sudo apt-get install -y clang-${{matrix.version}} llvm-${{matrix.version}}-dev libclang-common-${{matrix.version}}-dev libclang-${{matrix.version}}-dev libclang-cpp${{matrix.version}}-dev && git submodule init && git submodule update 19 | - name: Configure CMake 20 | run: cmake -B ${{github.workspace}}/build -DCMAKE_CXX_COMPILER=clang++-${{matrix.version}} -DLLVM_CONF=llvm-config-${{matrix.version}} -DUSE_MONOLITH_LIBTOOLING=ON 21 | - name: Build 22 | run: cmake --build ${{github.workspace}}/build 23 | - name: Test 24 | working-directory: ${{github.workspace}}/build 25 | run: ctest -C ${{env.BUILD_TYPE}} 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "cfe-extra"] 2 | path = cfe-extra 3 | url = https://github.com/bloodstalker/cfe-extra 4 | -------------------------------------------------------------------------------- /.gitpod.Dockerfile: -------------------------------------------------------------------------------- 1 | FROM gitpod/workspace-full 2 | 3 | # Install custom tools, runtimes, etc. 4 | # For example "bastet", a command-line tetris clone: 5 | # RUN brew install bastet 6 | # 7 | # More information: https://www.gitpod.io/docs/config-docker/ 8 | FROM ubuntu:20.04 9 | RUN apt update && apt upgrade -y 10 | ENV TERM=xterm-256color 11 | RUN DEBIAN_FRONTEND="noninteractive" apt-get -y install tzdata 12 | RUN apt install wget subversion gnupg2 software-properties-common make git xterm libffi7 -y 13 | 14 | RUN wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add - \ 15 | && add-apt-repository "deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-9 main" \ 16 | && apt-get update \ 17 | && apt-get install clang-9 llvm-9-dev libclang-common-9-dev libclang-9-dev libllvm9 -y 18 | 19 | RUN git clone https://github.com/bloodstalker/cgrep \ 20 | && cd cgrep \ 21 | && git submodule init \ 22 | && git submodule update \ 23 | && make CXX=clang-9 LLVM_CONF=llvm-config-9 24 | 25 | RUN mkdir devi 26 | WORKDIR /devi -------------------------------------------------------------------------------- /.gitpod.yml: -------------------------------------------------------------------------------- 1 | image: 2 | file: .gitpod.Dockerfile 3 | 4 | tasks: 5 | - init: make CXX=clang-9 LLVM_CONF=llvm-config-9 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | matrix: 2 | fast_finish: true 3 | include: 4 | - dist: bionic 5 | name: llvm7 6 | sudo: required 7 | language: cpp 8 | before_script: 9 | - sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y 10 | - sudo apt-get update -y 11 | - sudo apt-get install libstdc++-7-dev -y 12 | - sudo wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add - 13 | - sudo add-apt-repository "deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-7 main" 14 | - sudo apt-get update 15 | - sudo apt-get install clang-7 llvm-7-dev libclang-common-7-dev libclang-7-dev -y 16 | - git submodule init 17 | - git submodule update 18 | script: 19 | - make CXX=clang-7 LLVM_CONF=llvm-config-7 20 | after_success: 21 | bash run.sh 22 | - dist: bionic 23 | name: llvm8 24 | sudo: required 25 | language: cpp 26 | before_script: 27 | - sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y 28 | - sudo apt-get update -y 29 | - sudo apt-get install libstdc++-7-dev -y 30 | - sudo wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add - 31 | - sudo add-apt-repository "deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-8 main" 32 | - sudo apt-get update 33 | - sudo apt-get install clang-8 llvm-8-dev libclang-common-8-dev libclang-8-dev -y 34 | - git submodule init 35 | - git submodule update 36 | script: 37 | - make CXX=clang-8 LLVM_CONF=llvm-config-8 38 | after_success: 39 | bash run.sh 40 | - dist: bionic 41 | name: llvm9 42 | sudo: required 43 | language: cpp 44 | before_script: 45 | - sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y 46 | - sudo apt-get update -y 47 | - sudo apt-get install libstdc++-7-dev -y 48 | - sudo wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add - 49 | - sudo add-apt-repository "deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-9 main" 50 | - sudo apt-get update 51 | - sudo apt-get install clang-9 llvm-9-dev libclang-common-9-dev libclang-9-dev -y 52 | - git submodule init 53 | - git submodule update 54 | script: 55 | - make CXX=clang-9 LLVM_CONF=llvm-config-9 56 | after_success: 57 | bash run.sh 58 | - dist: bionic 59 | name: llvm10 60 | sudo: required 61 | language: cpp 62 | before_script: 63 | - sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y 64 | - sudo apt-get update -y 65 | - sudo apt-get install libstdc++-7-dev -y 66 | - sudo wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add - 67 | - sudo add-apt-repository "deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-10 main" 68 | - sudo apt-get update 69 | - sudo apt-get install clang-10 llvm-10-dev libclang-common-10-dev libclang-10-dev -y 70 | - git submodule init 71 | - git submodule update 72 | script: 73 | - make CXX=clang-10 LLVM_CONF=llvm-config-10 74 | after_success: 75 | bash run.sh 76 | - dist: bionic 77 | name: llvm11 78 | sudo: required 79 | language: cpp 80 | before_script: 81 | - sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y 82 | - sudo apt-get update -y 83 | - sudo apt-get install libstdc++-7-dev -y 84 | - sudo wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add - 85 | - sudo add-apt-repository "deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-11 main" 86 | - sudo apt-get update 87 | - sudo apt-get install clang-11 llvm-11-dev libclang-common-11-dev libclang-11-dev -y 88 | - git submodule init 89 | - git submodule update 90 | script: 91 | - make CXX=clang-11 LLVM_CONF=llvm-config-11 92 | after_success: 93 | bash run.sh 94 | - dist: bionic 95 | name: llvm12 96 | sudo: required 97 | language: cpp 98 | before_script: 99 | - sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y 100 | - sudo apt-get update -y 101 | - wget https://apt.llvm.org/llvm.sh 102 | - chmod +x llvm.sh 103 | - sudo ./llvm.sh 12 104 | - sudo apt-get install -y clang-12 llvm-12-dev libclang-common-12-dev libclang-12-dev 105 | - git submodule init 106 | - git submodule update 107 | script: 108 | - make CXX=clang-12 LLVM_CONF=llvm-config-12 109 | - dist: bionic 110 | name: llvm13 111 | sudo: required 112 | language: cpp 113 | before_script: 114 | - sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y 115 | - sudo apt-get update -y 116 | - wget https://apt.llvm.org/llvm.sh 117 | - chmod +x llvm.sh 118 | - sudo ./llvm.sh 13 119 | - sudo apt-get install -y clang-13 llvm-13-dev libclang-common-13-dev libclang-13-dev 120 | - git submodule init 121 | - git submodule update 122 | script: 123 | - make CXX=clang-13 LLVM_CONF=llvm-config-13 124 | - dist: bionic 125 | name: llvm14 126 | sudo: required 127 | language: cpp 128 | before_script: 129 | - sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y 130 | - sudo apt-get update -y 131 | - wget https://apt.llvm.org/llvm.sh 132 | - chmod +x llvm.sh 133 | - sudo ./llvm.sh 14 134 | - sudo apt-get install -y clang-14 llvm-14-dev libclang-common-14-dev libclang-14-dev 135 | - git submodule init 136 | - git submodule update 137 | script: 138 | - make CXX=clang-14 LLVM_CONF=llvm-config-14 139 | - dist: focal 140 | name: llvm15 141 | sudo: required 142 | language: cpp 143 | before_script: 144 | - sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y 145 | - sudo apt-get update -y 146 | - wget https://apt.llvm.org/llvm.sh 147 | - chmod +x llvm.sh 148 | - sudo ./llvm.sh 15 149 | - sudo apt-get install -y clang-15 llvm-15-dev libclang-common-15-dev libclang-15-dev 150 | - git submodule init 151 | - git submodule update 152 | script: 153 | - mkdir build && cd build && cmake ../ -DLLVM_CONF=llvm-config-15 -DCMAKE_CXX_COMPILER=clang++-15 -DUSE_MONOLITH_LIBTOOLING=OFF -DLLVM_PACKAGE_VERSION=15.0.0 && make 154 | - dist: focal 155 | name: llvm16 156 | sudo: required 157 | language: cpp 158 | before_script: 159 | - sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y 160 | - sudo apt-get update -y 161 | - wget https://apt.llvm.org/llvm.sh 162 | - chmod +x llvm.sh 163 | - sudo ./llvm.sh 16 164 | - sudo apt-get install -y clang-16 llvm-16-dev libclang-common-16-dev libclang-16-dev 165 | - git submodule init 166 | - git submodule update 167 | script: 168 | - mkdir build && cd build && cmake ../ -DLLVM_CONF=llvm-config-16 -DCMAKE_CXX_COMPILER=clang++-16 -DUSE_MONOLITH_LIBTOOLING=OFF -DLLVM_PACKAGE_VERSION=16.0.0 && make 169 | - dist: focal 170 | name: llvm17 171 | sudo: required 172 | language: cpp 173 | before_script: 174 | - sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y 175 | - sudo apt-get update -y 176 | - wget https://apt.llvm.org/llvm.sh 177 | - chmod +x llvm.sh 178 | - sudo ./llvm.sh 17 179 | - sudo apt-get install -y clang-17 llvm-17-dev libclang-common-17-dev libclang-17-dev 180 | - git submodule init 181 | - git submodule update 182 | script: 183 | - mkdir build && cd build && cmake ../ -DLLVM_CONF=llvm-config-17 -DCMAKE_CXX_COMPILER=clang++-17 -DUSE_MONOLITH_LIBTOOLING=OFF -DLLVM_PACKAGE_VERSION=17.0.0 && make 184 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | cmake_minimum_required(VERSION 3.14.5) 3 | project(cgrep VERSION 1.1) 4 | set(CMAKE_CXX_STANDARD 17) 5 | set(CMAKE_CXX_STANDARD_REQUIRED True) 6 | set(LLVM_CONF "" CACHE STRING "set the actual name of llvm-config, i.e. llvm-config-10") 7 | 8 | set(LLVM_CONF_TMP "${LLVM_CONF}") 9 | unset(LLVM_CONF CACHE) 10 | 11 | if(LLVM_CONF_TMP STREQUAL "") 12 | # If LLVM_CONF wasn't provided, search for it. 13 | find_program(LLVM_CONF NAMES llvm-config llvm-config-12 llvm-config-11 llvm-config-10 REQUIRED) 14 | else() 15 | # If LLVM_CONF was provided, check if the executable actually exists. 16 | find_program(LLVM_CONF NAMES "${LLVM_CONF_TMP}" REQUIRED) 17 | endif() 18 | unset(LLVM_CONF_TMP) 19 | 20 | function(CleanMessage) 21 | execute_process(COMMAND ${CMAKE_COMMAND} -E echo "${ARGN}") 22 | endfunction() 23 | 24 | execute_process(COMMAND ${LLVM_CONF} --includedir OUTPUT_VARIABLE LLVM_INC_DIR) 25 | string(REGEX REPLACE "\n$" "" LLVM_INC_DIR "${LLVM_INC_DIR}") 26 | execute_process(COMMAND ${LLVM_CONF} --cxxflags OUTPUT_VARIABLE LLVM_CXX_FLAGS) 27 | string(REGEX REPLACE "\n$" "" LLVM_CXX_FLAGS "${LLVM_CXX_FLAGS}") 28 | execute_process(COMMAND ${LLVM_CONF} --libdir OUTPUT_VARIABLE LLVM_LIB_DIR) 29 | string(REGEX REPLACE "\n" "" LLVM_LIB_DIR "${LLVM_LIB_DIR}") 30 | execute_process(COMMAND ${LLVM_CONF} --ldflags OUTPUT_VARIABLE LLVM_LD_FLAGS) 31 | string(REGEX REPLACE "\n" "" LLVM_LD_FLAGS "${LLVM_LD_FLAGS}") 32 | execute_process(COMMAND ${LLVM_CONF} --libs OUTPUT_VARIABLE LLVM_LIBS) 33 | string(REGEX REPLACE "\n$" "" LLVM_LIBS "${LLVM_LIBS}") 34 | string(REGEX REPLACE "^-l" "" LLVM_LIBS "${LLVM_LIBS}") 35 | execute_process(COMMAND ${LLVM_CONF} --system-libs OUTPUT_VARIABLE LLVM_SYS_LIBS) 36 | string(REGEX REPLACE "\n$" "" LLVM_SYS_LIBS "${LLVM_SYS_LIBS}") 37 | string(REGEX REPLACE "^-l" "" LLVM_SYS_LIBS "${LLVM_SYS_LIBS}") 38 | 39 | add_compile_options(${LLVM_CXX_FLAGS}) 40 | add_compile_options(-I${LLVM_INC_DIR}) 41 | 42 | set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/bin) 43 | set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}) 44 | set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}) 45 | 46 | add_link_options(${LLVM_LD_FLAGS}) 47 | add_link_options(-L${LLVM_LIB_DIR}) 48 | 49 | add_executable(cgrep cgrep.cpp ${CMAKE_SOURCE_DIR}/cfe-extra/cfe_extra.cpp) 50 | option(USE_MONOLITH_LIBTOOLING "use libtooling built into a single library" OFF) 51 | if (USE_MONOLITH_LIBTOOLING) 52 | target_link_libraries(cgrep clang-cpp) 53 | else() 54 | target_link_libraries(cgrep -Wl,--start-group clangAST clangAnalysis clangBasic clangDriver clangEdit clangFrontend clangFrontendTool clangLex clangParse clangSema clangEdit clangASTMatchers clangRewrite clangRewriteFrontend clangStaticAnalyzerFrontend clangStaticAnalyzerCheckers clangStaticAnalyzerCore clangSerialization clangToolingCore clangTooling stdc++ LLVMRuntimeDyld m -Wl,--end-group) 55 | endif() 56 | target_link_libraries(cgrep ${LLVM_SYS_LIBS}) 57 | target_link_libraries(cgrep ${LLVM_LIBS}) 58 | 59 | if(LLVM_PACKAGE_VERSION VERSION_EQUAL "15.0.0" OR LLVM_PACKAGE_VERSION VERSION_GREATER "15.0.0") 60 | target_link_libraries(cgrep clangSupport) 61 | endif() 62 | 63 | include_directories("${PROJECT_SOURCE_DIR}/cfe-extra") 64 | target_include_directories(cgrep PUBLIC "${PROJECT_BINARY_DIR}" "${PROJECT_SOURCE_DIR/cfe-extra}") 65 | 66 | -------------------------------------------------------------------------------- /CONTRIBUTORS.md: -------------------------------------------------------------------------------- 1 | ## Contributors 2 | 3 | The list is in chronological order:
4 | * bloodstalker 5 | * Yeger 6 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM debian:bullseye-slim 2 | 3 | RUN apt update && apt upgrade -y 4 | RUN apt install -y clang-11 llvm-11-dev libclang-common-11-dev libclang-11-dev libllvm11 5 | RUN apt install -y git make libstdc++6 -y 6 | 7 | RUN git clone https://github.com/bloodstalker/cgrep \ 8 | && cd cgrep \ 9 | && git submodule init \ 10 | && git submodule update \ 11 | && make CXX=clang-11 LLVM_CONF=llvm-config-11 12 | 13 | RUN rm -rf /var/apt/cache 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![example workflow](https://github.com/terminaldweller/cgrep/actions/workflows/cmake.yml/badge.svg) 2 | [![Codacy Badge](https://app.codacy.com/project/badge/Grade/a70e8f48e3214a4f97850b4a1d7c5686)](https://www.codacy.com/gh/terminaldweller/cgrep/dashboard?utm_source=github.com&utm_medium=referral&utm_content=terminaldweller/cgrep&utm_campaign=Badge_Grade) 3 | 4 | # cgrep 5 | 6 | cgrep is `grep` for C-family source files. 7 | 8 | You can write something like this: 9 | 10 | ```bash 11 | cgrep --regex [a-z]* --func -A 1 -B 1 myawesomecode.cpp 12 | ``` 13 | 14 | and it will match your regex against all function declarations, and will output the result, plus one line before and after the context. 15 | 16 | `cgrep` is implemented using Clang's libtooling libraries. 17 | 18 | ## Features 19 | 20 | - It's basically Clang regexing it's way through your C-family source-code. You have all the context you can ever need. 21 | - Can output whether to print the declaration of a match even if the match itself is not a declaration along with the matched result. 22 | - Can output matches in a script-friendly format which could be used in turn by a secondary script. 23 | 24 | ### Will cgrep try to implement all of the grep switches? 25 | 26 | The answer is no. The main distinction is that `cgrep` is only meant to work on C-family source files not text files. Most of `grep`'s switches don't apply to the usecase or provide almost no benefits at all.
27 | That being said, I might have missd something so you can always make suggestions in the form of a new issue. 28 | 29 | ### Will cgrep support a new switch that matches X? 30 | 31 | If it makes sense sure, but I want to be careful with what cgrep implements. If everything gets implemented, that is, cgrep implements every possible switch(well, a subset of "all"), we end up with an inferior version of `clang-query` that would be too slow to be of any use to anyone. So please keep in mind that I will have to draw the line somewhere. 32 | 33 | ## Building 34 | 35 | There are a couple of examples under `docker`. You can use those if you get stuck.
36 | 37 | ### Good Ole' Makefiles 38 | 39 | **NOTE: Good ole makefiles are no longer supported.**
40 | 41 | ### Cmake 42 | 43 | To do an out-of-source build simply do:
44 | 45 | ```bash 46 | git clone https://github.com/terminaldweller/cgrep 47 | cd cgrep 48 | git submodule init 49 | git submodule update 50 | mkdir build 51 | cmake ../ -DLLVM_CONF=llvm-config-15 -DCMAKE_CXX_COMPILER=clang++-15 -DUSE_MONOLITH_LIBTOOLING=ON 52 | make 53 | ``` 54 | 55 | The 4 variables denote the llvm-config executable name, the clang++ name and finally, the 3rd one tells cmake whether to build using the single c++ libtooling library or just use the old way with all the libtooling libraries. The last one lets cmake know which version of llvm/clang is being used.
56 | 57 | ## Usage 58 | 59 | A simple usage example: 60 | 61 | ```bash 62 | cgrep -A 1 -B 1 --func --declrefexpr --regex n[aA]m --nocolor --nodecl ./myawesomecode.cpp 63 | ``` 64 | 65 | In order for cgrep to work, you need to have a compilation database, tools like `cmake` can generate one for you.
66 | You can, by all means, run cgrep without a compilation database but whether that works or not really depends on your source file. Can you build your source file with clang without passing it any options? 67 | If the answer to that is yes, then you can just run cgrep without a compilation database like so:
68 | 69 | ```bash 70 | cgrep -A 1 -B 1 --func --declrefexpr --regex n[aA]m --nocolor --nodecl ./myawesomecode.cpp -- 71 | ``` 72 | 73 | the `--` at the end is an explicit way of saying that you will not be providing a compilation database. Newer versions of clang will try to still go through with the compilation even if there is no compilation database found. 74 | Otherwise you need a compilation database.
75 | 76 | Please do note that the regex will pass through both C++ and the regex engine, so if you would want to escape `\`, the regex you pass as the command line arg would be `\\\\` instead of the normal `\\`.
77 | If your build tool doesn't do that, you can just use [bear](https://github.com/rizsotto/Bear) or [scan-build](https://github.com/rizsotto/scan-build).
78 | You can also skip the compilation database altogether passing cgrep `--` after the input file name which means you have chosen not to pass it anything.
79 | You can pass the options by hand using `--extra-arg=` since cgrep is a clang instance so it recognizes every option clang has. 80 | As a general rule, if you're not going to pass cgrep a compilation database, it's always better to explicitly let cgrep know using `--`. Not doing so can result in instances when cgrep behaves in a way that you might not expect it.
81 | 82 | cgrep uses ANSI escape sequences for colors so your terminal should support those. In case your terminal does not support ANSI escape sequences or you don't want thos for any other reason, you can silence those using the `--nocolor` option. 83 | 84 | By default, cgrep will print out the declaration location for a match. In case you don't want those in the output, you can pass cgrep the `--nodecl` switch. 85 | 86 | You can use `--extra-arg=--std=` to tell cgrep which C-family language the source file is supposed to be in. 87 | 88 | ## Options 89 | 90 | Here's an option list, though it's usually not up-to-date.
91 | For an up-to-date list, you can run `cgrep --help` or look at the man page. 92 | 93 | ```bash 94 | -A= - Same as grep, how many lines after the matched line to print. Defaults to 0. 95 | -B= - Same as grep, how many lines before the matched line to print. Defaults to 0. 96 | --all - Turns on all switches other than nameddecl. 97 | --awk - Outputs location in a gawk friendly format, not meant for human consumption. Defaults to false. 98 | --call - Match function calls. 99 | --class - Match class declarations. 100 | --cxxcall - Match member function calls. 101 | --declrefexpr - Matches declrefexpr. 102 | --dir= - Recursively goes through all the files and directories. Assumes compilation databases are present for all source files. 103 | --extra-arg= - Additional argument to append to the compiler command line 104 | --extra-arg-before= - Additional argument to prepend to the compiler command line 105 | --func - Match functions. 106 | --header - Match headers in header inclusions. 107 | --macro - Match macro definitions. 108 | --mainfile - Match identifiers in the main file only. Defaults to true. 109 | --memfunc - Match member functions. 110 | --memvar - Match member variables. 111 | --nameddecl - Matches all named declarations. 112 | --nocolor - For terminals that don't support ANSI escape sequences. The default is to false. 113 | --nodecl - For switches that are not declarations, don't print declarations. Defaults to false. 114 | -p= - Build path 115 | --recorddecl - Match a record declaration. 116 | --regex= - The regex to match against. 117 | --struct - Match structures. 118 | --syshdr - Match identifiers in system header as well. Defaults to false. 119 | --union - Match unions. 120 | --var - Match variables. 121 | ``` 122 | 123 | `cgrep` is a clang tool, so it will accept all valid clang command line options. 124 | 125 | ## Known Issues 126 | 127 | `cgrep` complains that it cannot find `stddef.h` or some other similar header. If that happens to you , it's because cgrep can't find the clang built-in headers. run `llvm-config --libdir`, then head on to `clang`. Inside that directory you should see one(or maybe more) llvm/clang versions. Pick the one you used to build cgrep against. Inside that directory there will be a directory named `include`. Pass that to cgrep any way you see fit.
128 | Alternatively, `$(llvm-config --libdir)/clang/$(llvm-config --version)/include` should give the path cgrep needs to include. If you build your llvm/clang from upstream, this might not work. SVN builds will have the svn string attached to the version number.
129 | You could,for example, use `--extra-arg=-I/usr/lib/llvm-9/lib/clang/9.0.0/include` to call cgrep or you could just alias `cgrep` to `cgrep --extra-arg=-I/usr/lib/llvm-9/lib/clang/9.0.0/include`.
130 | 131 | `cgrep`, replaces the clang diagnosticConsumer with a simple one that only tells you there are erros during the compilation. You can get the normal clang output using the `--clangdiag` switch. The decision was made to declutter the output generated by cgrep. 132 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | 2 | environment: 3 | global: 4 | CYG_MIRROR: http://mirrors.kernel.org/sourceware/cygwin 5 | CYG_ROOT: C:\cygwin64 6 | CYG_SETUP: setup-x86_64.exe 7 | CYG_CACHE: '%CYG_ROOT%\var\cache\setup' 8 | CYG_BASH: '%CYG_ROOT%\bin\bash' 9 | 10 | install: 11 | #- '%CYG_ROOT%\%CYG_SETUP% -gnqINDo -R "%CYG_ROOT%" -s "%CYG_MIRROR%" -l "%CYG_CACHE%" -P make,libclang-devel,libllvm-devel,clang > NULL 2>&1' 12 | - '%CYG_ROOT%\%CYG_SETUP% -qI -R "%CYG_ROOT%" -s "%CYG_MIRROR%" -P make,libclang-devel,libllvm-devel,clang,libiconv-devel' 13 | - '%CYG_ROOT%\bin\cygcheck -dc cygwin' 14 | - 'cd %APPVEYOR_BUILD_FOLDER%' 15 | - 'git submodule init' 16 | - 'git submodule update' 17 | - 'clang++ --version' 18 | #- 'llvm-config --cppflags' 19 | - '%CYG_BASH% -lc "whereis clang++"' 20 | - '%CYG_BASH% -lc "which clang++"' 21 | - '%CYG_BASH% -lc "cygcheck -f $(which clang++)"' 22 | - '%CYG_BASH% -lc "/usr/bin/clang --version"' 23 | - '%CYG_BASH% -lc "/usr/bin/llvm-config --cppflags"' 24 | 25 | build_script: 26 | - '%CYG_ROOT%\bin\bash -lc "make -C $APPVEYOR_BUILD_FOLDER"' 27 | -------------------------------------------------------------------------------- /cgrep.cpp: -------------------------------------------------------------------------------- 1 | 2 | /*first line intentionally left blank.*/ 3 | /***********************************************************************************************/ 4 | //-*-c++-*- 5 | /*Copyright (C) 2018 Farzad Sadeghi 6 | * Licensed under GPL-3.0 7 | * */ 8 | /***********************************************************************************************/ 9 | /*included modules*/ 10 | #include "./cfe-extra/cfe_extra.h" 11 | #include "clang/AST/AST.h" 12 | #include "clang/AST/ASTConsumer.h" 13 | #include "clang/ASTMatchers/ASTMatchFinder.h" 14 | #include "clang/ASTMatchers/ASTMatchers.h" 15 | #include "clang/Basic/LLVM.h" 16 | #include "clang/Frontend/CompilerInstance.h" 17 | #include "clang/Frontend/FrontendActions.h" 18 | #include "clang/Lex/Lexer.h" 19 | #include "clang/Rewrite/Core/Rewriter.h" 20 | #include "clang/Tooling/CommonOptionsParser.h" 21 | #include "clang/Tooling/Tooling.h" 22 | #include "llvm/Support/raw_ostream.h" 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | /***********************************************************************************************/ 32 | /*used namespaces*/ 33 | using namespace llvm; 34 | using namespace clang; 35 | using namespace clang::ast_matchers; 36 | using namespace clang::driver; 37 | using namespace clang::tooling; 38 | /***********************************************************************************************/ 39 | namespace { 40 | static llvm::cl::OptionCategory CGrepCat("cgrep options"); 41 | cl::opt CO_RECURSIVE( 42 | "dir", 43 | cl::desc("Recursively goes through all the files and directories. Assumes " 44 | "compilation databases are present for all source files."), 45 | cl::init(""), cl::cat(CGrepCat), cl::Optional); 46 | cl::opt CO_REGEX("regex", cl::desc("The regex to match against."), 47 | cl::init(""), cl::cat(CGrepCat), 48 | cl::Optional); // done 49 | cl::opt 50 | CO_TRACE("trace", cl::desc("The variable that's going to be traced"), 51 | cl::init(""), cl::cat(CGrepCat), 52 | cl::Optional); // done 53 | cl::opt CO_FUNCTION("func", cl::desc("Match functions."), cl::init(false), 54 | cl::cat(CGrepCat), 55 | cl::Optional); // done 56 | cl::opt CO_MEM_FUNCTION("memfunc", cl::desc("Match member functions."), 57 | cl::init(false), cl::cat(CGrepCat), 58 | cl::Optional); // done 59 | cl::opt CO_VAR("var", cl::desc("Match variables."), cl::init(false), 60 | cl::cat(CGrepCat), cl::Optional); // done 61 | cl::opt CO_CALL("call", cl::desc("Match function calls."), 62 | cl::init(false), cl::cat(CGrepCat), cl::Optional); // done 63 | cl::opt CO_CXXCALL("cxxcall", cl::desc("Match member function calls."), 64 | cl::init(false), cl::cat(CGrepCat), 65 | cl::Optional); // done 66 | cl::opt CO_CFIELD("cfield", cl::desc("Match C field declarations."), 67 | cl::init(false), cl::cat(CGrepCat), 68 | cl::Optional); // done 69 | cl::opt CO_CLASS("class", cl::desc("Match class declarations."), 70 | cl::init(false), cl::cat(CGrepCat), 71 | cl::Optional); // done 72 | cl::opt CO_STRUCT("struct", cl::desc("Match structures."), 73 | cl::init(false), cl::cat(CGrepCat), 74 | cl::Optional); // done 75 | cl::opt CO_CXXFIELD("cxxfield", 76 | cl::desc("Match CXX field member declarations."), 77 | cl::init(false), cl::cat(CGrepCat), 78 | cl::Optional); // done 79 | cl::opt CO_RECORD("recorddecl", cl::desc("Match a record declaration."), 80 | cl::init(false), cl::cat(CGrepCat), 81 | cl::Optional); // done 82 | cl::opt CO_UNION("union", cl::desc("Match unions."), cl::init(false), 83 | cl::cat(CGrepCat), cl::Optional); // done 84 | cl::opt CO_MACRO("macro", cl::desc("Match macro definitions."), 85 | cl::init(false), cl::cat(CGrepCat), 86 | cl::Optional); // done 87 | cl::opt CO_CLANGDIAG("clangdiag", 88 | cl::desc("use clang's diagnostic consumer instead " 89 | "of the one that cgrep provide."), 90 | cl::init(false), cl::cat(CGrepCat), 91 | cl::Optional); // done 92 | cl::opt CO_HEADER("header", 93 | cl::desc("Match headers in header inclusions."), 94 | cl::init(false), cl::cat(CGrepCat), 95 | cl::Optional); // done 96 | cl::opt CO_ALL("all", 97 | cl::desc("Turns on all switches other than nameddecl."), 98 | cl::init(false), cl::cat(CGrepCat), cl::Optional); // done 99 | cl::opt CO_NAMEDDECL("nameddecl", 100 | cl::desc("Matches all named declarations."), 101 | cl::init(false), cl::cat(CGrepCat), 102 | cl::Optional); // done 103 | cl::opt CO_DECLREFEXPR("declrefexpr", cl::desc("Matches declrefexpr."), 104 | cl::init(false), cl::cat(CGrepCat), 105 | cl::Optional); // done 106 | cl::opt 107 | CO_AWK("awk", 108 | cl::desc("Outputs location in a gawk friendly format, not meant for " 109 | "human consumption. Defaults to false."), 110 | cl::init(false), cl::cat(CGrepCat), cl::Optional); // done 111 | cl::opt 112 | CO_NOCOLOR("nocolor", 113 | cl::desc("For terminals that don't support ANSI escape " 114 | "sequences. The default is to false."), 115 | cl::init(false), cl::cat(CGrepCat), 116 | cl::Optional); // done 117 | cl::opt 118 | CO_NODECL("nodecl", 119 | cl::desc("For switches that are not declarations, don't print " 120 | "declarations. Defaults to false."), 121 | cl::init(false), cl::cat(CGrepCat), cl::Optional); // done 122 | cl::opt CO_SYSHDR( 123 | "syshdr", 124 | cl::desc("Match identifiers in system header as well. Defaults to true."), 125 | cl::init(false), cl::cat(CGrepCat), 126 | cl::Optional); // done 127 | cl::opt CO_MAINFILE( 128 | "mainfile", 129 | cl::desc("Match identifiers in the main file only. Defaults to true."), 130 | cl::init(true), cl::cat(CGrepCat), 131 | cl::Optional); // done 132 | cl::opt CO_A("A", 133 | cl::desc("Same as grep, how many lines after the matched " 134 | "line to print. Defaults to 0."), 135 | cl::init(0), cl::cat(CGrepCat), cl::Optional); // done 136 | cl::opt CO_B("B", 137 | cl::desc("Same as grep, how many lines before the matched " 138 | "line to print. Defaults to 0."), 139 | cl::init(0), cl::cat(CGrepCat), cl::Optional); // done 140 | cl::opt 141 | CO_C("C", 142 | cl::desc("Same as grep, how many lines before and after the matched " 143 | "line to print. Defaults to 0."), 144 | cl::init(0), cl::cat(CGrepCat), cl::Optional); // done 145 | } // namespace 146 | /***********************************************************************************************/ 147 | #if 1 148 | #define REGEX_PP(RX_STR) RX_STR 149 | #endif 150 | #if 0 151 | #define REGEX_PP(RX_STR) regex_preprocessor(RX_STR) 152 | #endif 153 | 154 | #if __clang_major__ <= 6 155 | #define DEVI_GETLOCSTART getLocStart 156 | #define DEVI_GETLOCEND getLocEnd 157 | #elif __clang_major__ >= 7 158 | #define DEVI_GETLOCSTART getBeginLoc 159 | #define DEVI_GETLOCEND getEndLoc 160 | #endif 161 | 162 | #if __clang_major__ >= 12 163 | #define AST_TYPE_TRAITS clang 164 | #else 165 | #define AST_TYPE_TRAITS clang::ast_type_traits 166 | #endif 167 | 168 | #define RED "\033[1;31m" 169 | #define CYAN "\033[1;36m" 170 | #define GREEN "\033[1;32m" 171 | #define BLUE "\033[1;34m" 172 | #define BLACK "\033[1;30m" 173 | #define BROWN "\033[1;33m" 174 | #define MAGENTA "\033[1;35m" 175 | #define GRAY "\033[1;37m" 176 | #define DARKGRAY "\033[1;30m" 177 | #define YELLOW "\033[1;33m" 178 | #define NORMAL "\033[0m" 179 | #define CLEAR "\033[2J" 180 | 181 | #define CC_RED (CO_NOCOLOR == true ? "" : RED) 182 | #define CC_CYAN (CO_NOCOLOR == true ? "" : CYAN) 183 | #define CC_GREEN (CO_NOCOLOR == true ? "" : GREEN) 184 | #define CC_BLUE (CO_NOCOLOR == true ? "" : BLUE) 185 | #define CC_BLACK (CO_NOCOLOR == true ? "" : BLACK) 186 | #define CC_BROWN (CO_NOCOLOR == true ? "" : BROWN) 187 | #define CC_MAGENTA (CO_NOCOLOR == true ? "" : MAGENTA) 188 | #define CC_GRAY (CO_NOCOLOR == true ? "" : GRAY) 189 | #define CC_DARKGRAY (CO_NOCOLOR == true ? "" : DARKGRAY) 190 | #define CC_YELLOW (CO_NOCOLOR == true ? "" : YELLOW) 191 | #define CC_NORMAL (CO_NOCOLOR == true ? "" : NORMAL) 192 | #define CC_CLEAR (CO_NOCOLOR == true ? "" : CLEAR) 193 | /***********************************************************************************************/ 194 | static std::string get_line_from_file(SourceManager &SM, 195 | const MatchFinder::MatchResult &MR, 196 | SourceRange SR) { 197 | std::string Result = ""; 198 | 199 | std::ifstream mainfile; 200 | std::string mainfile_str = MR.SourceManager->getFilename(SR.getBegin()).str(); 201 | mainfile.open(mainfile_str); 202 | auto linenumber = MR.SourceManager->getSpellingLineNumber(SR.getBegin()); 203 | 204 | std::string line; 205 | unsigned line_nu = 0; 206 | 207 | while (getline(mainfile, line)) { 208 | line_nu++; 209 | if (line_nu == linenumber) { 210 | Result = line; 211 | std::cout << CC_GREEN << "\n" 212 | << mainfile_str << ":" << linenumber << ":" << line 213 | << "\t <---declared here" << CC_NORMAL << "\n"; 214 | } 215 | } 216 | 217 | return Result; 218 | } 219 | 220 | /** 221 | * @brief does some preprocessing on the regex string we get as input 222 | * @param rx_str 223 | * @return the preprocessed string 224 | */ 225 | std::string regex_preprocessor(const std::string &rx_str) { 226 | std::string ret_rx_str; 227 | return rx_str; 228 | } 229 | 230 | bool regex_handler(std::string rx_str, std::string identifier_name) { 231 | std::regex rx(rx_str); 232 | std::smatch result; 233 | return std::regex_search(identifier_name, result, rx); 234 | } 235 | 236 | /** 237 | * @brief all print outs pass through here 238 | * 239 | * @param MR match result 240 | * @param SR source range for the matched result 241 | * @param SM sourcemanager 242 | * @param isdecl is the matched result a declaration 243 | * @param DTN the matched result cast to a dynamically typed node 244 | */ 245 | void output_handler(const MatchFinder::MatchResult &MR, SourceRange SR, 246 | SourceManager &SM, bool isdecl, 247 | AST_TYPE_TRAITS::DynTypedNode &DTN) { 248 | std::ifstream mainfile; 249 | mainfile.open(MR.SourceManager->getFilename(SR.getBegin()).str()); 250 | auto linenumber = MR.SourceManager->getSpellingLineNumber(SR.getBegin()); 251 | auto columnnumber_start = 252 | MR.SourceManager->getSpellingColumnNumber(SR.getBegin()) - 1; 253 | auto columnnumber_end = 254 | MR.SourceManager->getSpellingColumnNumber(SR.getEnd()) - 1; 255 | if (CO_AWK) { 256 | std::cout << CC_MAGENTA << SR.getBegin().printToString(SM) << ":" 257 | << SR.getEnd().printToString(SM) << CC_NORMAL << "\n"; 258 | std::cout << CC_RED << MR.SourceManager->getFilename(SR.getBegin()).str() 259 | << ":" << linenumber << ":" << columnnumber_start << CC_NORMAL; 260 | } else { 261 | unsigned line_range_begin; 262 | unsigned line_range_end; 263 | if (0 >= CO_C) { 264 | line_range_begin = linenumber - CO_C; 265 | line_range_end = linenumber + CO_C; 266 | } 267 | line_range_begin = linenumber - CO_B; 268 | line_range_end = linenumber + CO_A; 269 | std::string line; 270 | unsigned line_nu = 0; 271 | while (getline(mainfile, line)) { 272 | line_nu++; 273 | if (line_nu >= line_range_begin && line_nu <= line_range_end) { 274 | if (line_nu == linenumber) { 275 | std::cout << CC_RED 276 | << MR.SourceManager->getFilename(SR.getBegin()).str() << ":" 277 | << linenumber << ":" << columnnumber_start << ":" 278 | << CC_NORMAL; 279 | for (unsigned i = 0; i < line.length(); ++i) { 280 | if (i >= columnnumber_start && i <= columnnumber_end) { 281 | std::cout << CC_RED << line[i] << CC_NORMAL; 282 | } else { 283 | std::cout << line[i]; 284 | } 285 | } 286 | if (!CO_NODECL) { 287 | if (!isdecl) { 288 | const NamedDecl *ND = DTN.get(); 289 | if (nullptr != ND) { 290 | SourceRange ND_SR = ND->getSourceRange(); 291 | get_line_from_file(SM, MR, ND_SR); 292 | } 293 | 294 | const CallExpr *CE = DTN.get(); 295 | if (nullptr != CE) { 296 | SourceRange CE_SR = CE->getDirectCallee()->getSourceRange(); 297 | get_line_from_file(SM, MR, CE_SR); 298 | } 299 | } 300 | } else { 301 | std::cout << line << "\n"; 302 | } 303 | } else { 304 | std::cout << line << "\n"; 305 | } 306 | } 307 | } 308 | } 309 | std::cout << "\n"; 310 | mainfile.close(); 311 | } 312 | /** 313 | * @brief Gets the list of all directories and sub-directories starting from a 314 | * base directory. 315 | * @param _path where the base directory is. 316 | * @return Returns the list of all found dirs. 317 | */ 318 | std::vector listDirs(std::string path) { 319 | std::vector dummy; 320 | DIR *dir; 321 | if ((dir = opendir(path.c_str())) != nullptr) { 322 | struct dirent *ent; 323 | while ((ent = readdir(dir)) != nullptr) { 324 | std::cout << "name: " << ent->d_name << "\ttype:" << int(ent->d_type) 325 | << "\n"; 326 | if (ent->d_type == DT_DIR) { 327 | std::cout << ent->d_name << "\n"; 328 | } 329 | dummy.push_back(ent->d_name); 330 | } 331 | } else { 332 | perror("could not open directory."); 333 | } 334 | return dummy; 335 | } 336 | /***********************************************************************************************/ 337 | class FunctionHandler : public MatchFinder::MatchCallback { 338 | public: 339 | explicit FunctionHandler(Rewriter &Rewrite) : Rewrite(Rewrite) {} 340 | 341 | virtual void run(const MatchFinder::MatchResult &MR) { 342 | const FunctionDecl *FD = 343 | MR.Nodes.getNodeAs("funcdecl"); 344 | if (FD) { 345 | DeclarationNameInfo DNI = FD->getNameInfo(); 346 | SourceRange SR = DNI.getSourceRange(); 347 | SourceLocation SL = SR.getBegin(); 348 | CheckSLValidity(SL); 349 | SL = Devi::SourceLocationHasMacro(SL, Rewrite, "start"); 350 | if (Devi::IsTheMatchInSysHeader(CO_SYSHDR, MR, SL)) 351 | return void(); 352 | if (!Devi::IsTheMatchInMainFile(CO_MAINFILE, MR, SL)) 353 | return void(); 354 | std::string name = FD->getNameAsString(); 355 | if (regex_handler(REGEX_PP(CO_REGEX), name)) { 356 | AST_TYPE_TRAITS::DynTypedNode DNode = 357 | AST_TYPE_TRAITS::DynTypedNode::create(*FD); 358 | auto StartLocation = FD->getLocation(); 359 | auto EndLocation = StartLocation.getLocWithOffset(name.size() - 1); 360 | auto Range = SourceRange(StartLocation, EndLocation); 361 | output_handler(MR, Range, *MR.SourceManager, 362 | FD->isThisDeclarationADefinition(), DNode); 363 | } 364 | } 365 | } 366 | 367 | private: 368 | Rewriter &Rewrite [[maybe_unused]]; 369 | }; 370 | /***********************************************************************************************/ 371 | class FieldHandler : public MatchFinder::MatchCallback { 372 | public: 373 | explicit FieldHandler(Rewriter &Rewrite) : Rewrite(Rewrite) {} 374 | 375 | virtual void run(const MatchFinder::MatchResult &MR) { 376 | const FieldDecl *FD = MR.Nodes.getNodeAs("fielddecl"); 377 | if (FD) { 378 | SourceRange SR = FD->getSourceRange(); 379 | SourceLocation SL = SR.getBegin(); 380 | CheckSLValidity(SL); 381 | SL = Devi::SourceLocationHasMacro(SL, Rewrite, "start"); 382 | if (Devi::IsTheMatchInSysHeader(CO_SYSHDR, MR, SL)) 383 | return void(); 384 | if (!Devi::IsTheMatchInMainFile(CO_MAINFILE, MR, SL)) 385 | return void(); 386 | std::string name = FD->getNameAsString(); 387 | if (regex_handler(REGEX_PP(CO_REGEX), name)) { 388 | AST_TYPE_TRAITS::DynTypedNode DNode = 389 | AST_TYPE_TRAITS::DynTypedNode::create(*FD); 390 | auto StartLocation = FD->getLocation(); 391 | auto EndLocation = StartLocation.getLocWithOffset(name.size() - 1); 392 | auto Range = SourceRange(StartLocation, EndLocation); 393 | output_handler(MR, Range, *MR.SourceManager, true, DNode); 394 | } 395 | } 396 | } 397 | 398 | private: 399 | Rewriter &Rewrite [[maybe_unused]]; 400 | }; 401 | /***********************************************************************************************/ 402 | class CXXMethodHandler : public MatchFinder::MatchCallback { 403 | public: 404 | explicit CXXMethodHandler(Rewriter &Rewrite) : Rewrite(Rewrite) {} 405 | 406 | virtual void run(const MatchFinder::MatchResult &MR) { 407 | const CXXMethodDecl *MD = 408 | MR.Nodes.getNodeAs("cxxmethoddecl"); 409 | if (MD) { 410 | DeclarationNameInfo DNI = MD->getNameInfo(); 411 | SourceRange SR = DNI.getSourceRange(); 412 | SourceLocation SL = SR.getBegin(); 413 | CheckSLValidity(SL); 414 | SL = Devi::SourceLocationHasMacro(SL, Rewrite, "start"); 415 | if (Devi::IsTheMatchInSysHeader(CO_SYSHDR, MR, SL)) 416 | return void(); 417 | if (!Devi::IsTheMatchInMainFile(CO_MAINFILE, MR, SL)) 418 | return void(); 419 | std::string name = MD->getNameAsString(); 420 | if (regex_handler(REGEX_PP(CO_REGEX), name)) { 421 | AST_TYPE_TRAITS::DynTypedNode DNode = 422 | AST_TYPE_TRAITS::DynTypedNode::create(*MD); 423 | auto StartLocation = MD->getLocation(); 424 | auto EndLocation = StartLocation.getLocWithOffset(name.size() - 1); 425 | auto Range = SourceRange(StartLocation, EndLocation); 426 | output_handler(MR, Range, *MR.SourceManager, 427 | MD->isThisDeclarationADefinition(), DNode); 428 | } 429 | } 430 | } 431 | 432 | private: 433 | Rewriter &Rewrite [[maybe_unused]]; 434 | }; 435 | /***********************************************************************************************/ 436 | class VDecl : public MatchFinder::MatchCallback { 437 | public: 438 | explicit VDecl(Rewriter &Rewrite) : Rewrite(Rewrite) {} 439 | 440 | virtual void run(const MatchFinder::MatchResult &MR) { 441 | const VarDecl *VD = MR.Nodes.getNodeAs("vardecl"); 442 | if (VD) { 443 | SourceRange SR = VD->getSourceRange(); 444 | SourceLocation SL = SR.getBegin(); 445 | CheckSLValidity(SL); 446 | SL = Devi::SourceLocationHasMacro(SL, Rewrite, "start"); 447 | if (Devi::IsTheMatchInSysHeader(CO_SYSHDR, MR, SL)) 448 | return void(); 449 | if (!Devi::IsTheMatchInMainFile(CO_MAINFILE, MR, SL)) 450 | return void(); 451 | std::string name = VD->getNameAsString(); 452 | if (regex_handler(REGEX_PP(CO_REGEX), name)) { 453 | AST_TYPE_TRAITS::DynTypedNode DNode = 454 | AST_TYPE_TRAITS::DynTypedNode::create(*VD); 455 | auto StartLocation = VD->getLocation(); 456 | auto EndLocation = StartLocation.getLocWithOffset(name.size() - 1); 457 | auto Range = SourceRange(StartLocation, EndLocation); 458 | output_handler(MR, Range, *MR.SourceManager, true, DNode); 459 | } 460 | } 461 | } 462 | 463 | private: 464 | Rewriter &Rewrite [[maybe_unused]]; 465 | }; 466 | /***********************************************************************************************/ 467 | class ClassDecl : public MatchFinder::MatchCallback { 468 | public: 469 | explicit ClassDecl(Rewriter &Rewrite) : Rewrite(Rewrite) {} 470 | 471 | virtual void run(const MatchFinder::MatchResult &MR) { 472 | const RecordDecl *RD = MR.Nodes.getNodeAs("classdecl"); 473 | if (RD) { 474 | SourceRange SR = RD->getSourceRange(); 475 | SourceLocation SL = SR.getBegin(); 476 | CheckSLValidity(SL); 477 | SL = Devi::SourceLocationHasMacro(SL, Rewrite, "start"); 478 | if (Devi::IsTheMatchInSysHeader(CO_SYSHDR, MR, SL)) 479 | return void(); 480 | if (!Devi::IsTheMatchInMainFile(CO_MAINFILE, MR, SL)) 481 | return void(); 482 | std::string name = RD->getNameAsString(); 483 | if (regex_handler(REGEX_PP(CO_REGEX), name)) { 484 | AST_TYPE_TRAITS::DynTypedNode DNode = 485 | AST_TYPE_TRAITS::DynTypedNode::create(*RD); 486 | auto StartLocation = RD->getLocation(); 487 | auto EndLocation = StartLocation.getLocWithOffset(name.size() - 1); 488 | auto Range = SourceRange(StartLocation, EndLocation); 489 | output_handler(MR, Range, *MR.SourceManager, true, DNode); 490 | } 491 | } 492 | } 493 | 494 | private: 495 | Rewriter &Rewrite [[maybe_unused]]; 496 | }; 497 | /***********************************************************************************************/ 498 | class StructHandler : public MatchFinder::MatchCallback { 499 | public: 500 | explicit StructHandler(Rewriter &Rewrite) : Rewrite(Rewrite) {} 501 | 502 | virtual void run(const MatchFinder::MatchResult &MR) { 503 | const RecordDecl *RD = MR.Nodes.getNodeAs("structdecl"); 504 | if (RD) { 505 | SourceRange SR = RD->getSourceRange(); 506 | SourceLocation SL = SR.getBegin(); 507 | CheckSLValidity(SL); 508 | SL = Devi::SourceLocationHasMacro(SL, Rewrite, "start"); 509 | if (Devi::IsTheMatchInSysHeader(CO_SYSHDR, MR, SL)) 510 | return void(); 511 | if (!Devi::IsTheMatchInMainFile(CO_MAINFILE, MR, SL)) 512 | return void(); 513 | std::string name = RD->getNameAsString(); 514 | if (regex_handler(REGEX_PP(CO_REGEX), name)) { 515 | AST_TYPE_TRAITS::DynTypedNode DNode = 516 | AST_TYPE_TRAITS::DynTypedNode::create(*RD); 517 | auto StartLocation = RD->getLocation(); 518 | auto EndLocation = StartLocation.getLocWithOffset(name.size() - 1); 519 | auto Range = SourceRange(StartLocation, EndLocation); 520 | output_handler(MR, Range, *MR.SourceManager, true, DNode); 521 | } 522 | } 523 | } 524 | 525 | private: 526 | Rewriter &Rewrite [[maybe_unused]]; 527 | }; 528 | /***********************************************************************************************/ 529 | class UnionHandler : public MatchFinder::MatchCallback { 530 | public: 531 | explicit UnionHandler(Rewriter &Rewrite) : Rewrite(Rewrite) {} 532 | 533 | virtual void run(const MatchFinder::MatchResult &MR) { 534 | const RecordDecl *RD = MR.Nodes.getNodeAs("uniondecl"); 535 | if (RD) { 536 | SourceRange SR = RD->getSourceRange(); 537 | SourceLocation SL = SR.getBegin(); 538 | CheckSLValidity(SL); 539 | SL = Devi::SourceLocationHasMacro(SL, Rewrite, "start"); 540 | if (Devi::IsTheMatchInSysHeader(CO_SYSHDR, MR, SL)) 541 | return void(); 542 | if (!Devi::IsTheMatchInMainFile(CO_MAINFILE, MR, SL)) 543 | return void(); 544 | std::string name = RD->getNameAsString(); 545 | if (regex_handler(REGEX_PP(CO_REGEX), name)) { 546 | AST_TYPE_TRAITS::DynTypedNode DNode = 547 | AST_TYPE_TRAITS::DynTypedNode::create(*RD); 548 | auto StartLocation = RD->getLocation(); 549 | auto EndLocation = StartLocation.getLocWithOffset(name.size() - 1); 550 | auto Range = SourceRange(StartLocation, EndLocation); 551 | output_handler(MR, Range, *MR.SourceManager, true, DNode); 552 | } 553 | } 554 | } 555 | 556 | private: 557 | Rewriter &Rewrite [[maybe_unused]]; 558 | }; 559 | /***********************************************************************************************/ 560 | class NamedDeclHandler : public MatchFinder::MatchCallback { 561 | public: 562 | explicit NamedDeclHandler(Rewriter &Rewrite) : Rewrite(Rewrite) {} 563 | 564 | virtual void run(const MatchFinder::MatchResult &MR) { 565 | const NamedDecl *ND = MR.Nodes.getNodeAs("namedecl"); 566 | if (ND) { 567 | SourceRange SR = ND->getSourceRange(); 568 | SourceLocation SL = SR.getBegin(); 569 | CheckSLValidity(SL); 570 | SL = Devi::SourceLocationHasMacro(SL, Rewrite, "start"); 571 | if (Devi::IsTheMatchInSysHeader(CO_SYSHDR, MR, SL)) 572 | return void(); 573 | if (!Devi::IsTheMatchInMainFile(CO_MAINFILE, MR, SL)) 574 | return void(); 575 | std::string name = ND->getNameAsString(); 576 | if (regex_handler(REGEX_PP(CO_REGEX), name)) { 577 | AST_TYPE_TRAITS::DynTypedNode DNode = 578 | AST_TYPE_TRAITS::DynTypedNode::create(*ND); 579 | auto StartLocation = ND->getLocation(); 580 | auto EndLocation = StartLocation.getLocWithOffset(name.size() - 1); 581 | auto Range = SourceRange(StartLocation, EndLocation); 582 | output_handler(MR, Range, *MR.SourceManager, true, DNode); 583 | } 584 | } 585 | } 586 | 587 | private: 588 | Rewriter &Rewrite [[maybe_unused]]; 589 | }; 590 | /***********************************************************************************************/ 591 | class DeclRefExprHandler : public MatchFinder::MatchCallback { 592 | public: 593 | explicit DeclRefExprHandler(Rewriter &Rewrite) : Rewrite(Rewrite) {} 594 | 595 | virtual void run(const MatchFinder::MatchResult &MR) { 596 | const DeclRefExpr *DRE = 597 | MR.Nodes.getNodeAs("declrefexpr"); 598 | if (DRE) { 599 | const NamedDecl *ND = DRE->getFoundDecl(); 600 | std::string name = ND->getNameAsString(); 601 | SourceLocation SL = DRE->DEVI_GETLOCSTART(); 602 | SL = Devi::SourceLocationHasMacro(SL, Rewrite, "start"); 603 | SourceLocation SLE = SL.getLocWithOffset(name.length() - 1); 604 | // SourceLocation SLE = DRE->DEVI_GETLOCEND(); 605 | CheckSLValidity(SL); 606 | if (Devi::IsTheMatchInSysHeader(CO_SYSHDR, MR, SL)) 607 | return void(); 608 | if (!Devi::IsTheMatchInMainFile(CO_MAINFILE, MR, SL)) 609 | return void(); 610 | if (regex_handler(REGEX_PP(CO_REGEX), name)) { 611 | AST_TYPE_TRAITS::DynTypedNode DTN = 612 | AST_TYPE_TRAITS::DynTypedNode::create(*ND); 613 | auto StartLocation = ND->getLocation(); 614 | auto EndLocation = StartLocation.getLocWithOffset(name.size() - 1); 615 | auto Range = SourceRange(StartLocation, EndLocation); 616 | output_handler(MR, SourceRange(SL, SLE), *MR.SourceManager, false, DTN); 617 | } 618 | } 619 | } 620 | 621 | private: 622 | Rewriter &Rewrite [[maybe_unused]]; 623 | }; 624 | /***********************************************************************************************/ 625 | class CallExprHandler : public MatchFinder::MatchCallback { 626 | public: 627 | explicit CallExprHandler(Rewriter &Rewrite) : Rewrite(Rewrite) {} 628 | 629 | virtual void run(const MatchFinder::MatchResult &MR) { 630 | const CallExpr *CE = MR.Nodes.getNodeAs("callexpr"); 631 | if (CE) { 632 | SourceLocation SL = CE->DEVI_GETLOCSTART(); 633 | SourceLocation SLE = CE->DEVI_GETLOCEND(); 634 | CheckSLValidity(SL); 635 | SL = Devi::SourceLocationHasMacro(SL, Rewrite, "start"); 636 | if (Devi::IsTheMatchInSysHeader(CO_SYSHDR, MR, SL)) 637 | return void(); 638 | if (!Devi::IsTheMatchInMainFile(CO_MAINFILE, MR, SL)) 639 | return void(); 640 | const NamedDecl *ND = CE->getDirectCallee(); 641 | if (ND == nullptr) { 642 | return void(); 643 | } 644 | std::string name = ND->getNameAsString(); 645 | if (regex_handler(REGEX_PP(CO_REGEX), name)) { 646 | AST_TYPE_TRAITS::DynTypedNode DTN = 647 | AST_TYPE_TRAITS::DynTypedNode::create(*CE); 648 | auto StartLocation = CE->getExprLoc(); 649 | auto EndLocation = StartLocation.getLocWithOffset(name.size() - 1); 650 | auto Range = SourceRange(StartLocation, EndLocation); 651 | output_handler(MR, Range, *MR.SourceManager, false, DTN); 652 | } 653 | } 654 | } 655 | 656 | private: 657 | Rewriter &Rewrite [[maybe_unused]]; 658 | }; 659 | /***********************************************************************************************/ 660 | class CXXCallExprHandler : public MatchFinder::MatchCallback { 661 | public: 662 | explicit CXXCallExprHandler(Rewriter &Rewrite) : Rewrite(Rewrite) {} 663 | 664 | virtual void run(const MatchFinder::MatchResult &MR) { 665 | const CXXMemberCallExpr *CE = 666 | MR.Nodes.getNodeAs("cxxcallexpr"); 667 | if (CE) { 668 | SourceRange SR = CE->getSourceRange(); 669 | SourceLocation SL = SR.getBegin(); 670 | CheckSLValidity(SL); 671 | SL = Devi::SourceLocationHasMacro(SL, Rewrite, "start"); 672 | if (Devi::IsTheMatchInSysHeader(CO_SYSHDR, MR, SL)) 673 | return void(); 674 | if (!Devi::IsTheMatchInMainFile(CO_MAINFILE, MR, SL)) 675 | return void(); 676 | const NamedDecl *ND = CE->getDirectCallee(); 677 | if (ND == nullptr) { 678 | return void(); 679 | } 680 | std::string name = ND->getNameAsString(); 681 | if (regex_handler(REGEX_PP(CO_REGEX), name)) { 682 | AST_TYPE_TRAITS::DynTypedNode DNode = 683 | AST_TYPE_TRAITS::DynTypedNode::create(*CE); 684 | auto StartLocation = CE->getExprLoc(); 685 | auto EndLocation = StartLocation.getLocWithOffset(name.size() - 1); 686 | auto Range = SourceRange(StartLocation, EndLocation); 687 | output_handler(MR, Range, *MR.SourceManager, false, DNode); 688 | } 689 | } 690 | } 691 | 692 | private: 693 | Rewriter &Rewrite [[maybe_unused]]; 694 | }; 695 | /***********************************************************************************************/ 696 | class RecordFieldHandler : public MatchFinder::MatchCallback { 697 | public: 698 | explicit RecordFieldHandler(Rewriter &Rewrite) : Rewrite(Rewrite) {} 699 | 700 | virtual void run(const MatchFinder::MatchResult &MR) { 701 | const FieldDecl *FD = 702 | MR.Nodes.getNodeAs("recordfielddecl"); 703 | if (FD) { 704 | SourceRange SR = FD->getSourceRange(); 705 | SourceLocation SL = SR.getBegin(); 706 | CheckSLValidity(SL); 707 | SL = Devi::SourceLocationHasMacro(SL, Rewrite, "start"); 708 | if (Devi::IsTheMatchInSysHeader(CO_SYSHDR, MR, SL)) 709 | return void(); 710 | if (!Devi::IsTheMatchInMainFile(CO_MAINFILE, MR, SL)) 711 | return void(); 712 | std::string name = FD->getNameAsString(); 713 | if (regex_handler(REGEX_PP(CO_REGEX), name)) { 714 | AST_TYPE_TRAITS::DynTypedNode DNode = 715 | AST_TYPE_TRAITS::DynTypedNode::create(*FD); 716 | auto StartLocation = FD->getLocation(); 717 | auto EndLocation = StartLocation.getLocWithOffset(name.size() - 1); 718 | auto Range = SourceRange(StartLocation, EndLocation); 719 | output_handler(MR, Range, *MR.SourceManager, true, DNode); 720 | } 721 | } 722 | } 723 | 724 | private: 725 | Rewriter &Rewrite [[maybe_unused]]; 726 | }; 727 | /***********************************************************************************************/ 728 | class RecordHandler : public MatchFinder::MatchCallback { 729 | public: 730 | explicit RecordHandler(Rewriter &Rewrite) : Rewrite(Rewrite) {} 731 | 732 | virtual void run(const MatchFinder::MatchResult &MR) { 733 | const RecordDecl *RD = MR.Nodes.getNodeAs("recorddecl"); 734 | if (RD) { 735 | SourceRange SR = RD->getSourceRange(); 736 | SourceLocation SL = SR.getBegin(); 737 | CheckSLValidity(SL); 738 | SL = Devi::SourceLocationHasMacro(SL, Rewrite, "start"); 739 | if (Devi::IsTheMatchInSysHeader(CO_SYSHDR, MR, SL)) 740 | return void(); 741 | if (!Devi::IsTheMatchInMainFile(CO_MAINFILE, MR, SL)) 742 | return void(); 743 | std::string name = RD->getNameAsString(); 744 | if (regex_handler(REGEX_PP(CO_REGEX), name)) { 745 | AST_TYPE_TRAITS::DynTypedNode DNode = 746 | AST_TYPE_TRAITS::DynTypedNode::create(*RD); 747 | auto StartLocation = RD->getLocation(); 748 | auto EndLocation = StartLocation.getLocWithOffset(name.size() - 1); 749 | auto Range = SourceRange(StartLocation, EndLocation); 750 | output_handler(MR, Range, *MR.SourceManager, true, DNode); 751 | } 752 | } 753 | } 754 | 755 | private: 756 | Rewriter &Rewrite [[maybe_unused]]; 757 | }; 758 | /***********************************************************************************************/ 759 | class TraceVarHandlerSub : public MatchFinder::MatchCallback { 760 | public: 761 | explicit TraceVarHandlerSub(Rewriter &Rewrite) : Rewrite(Rewrite) {} 762 | 763 | virtual void run(const MatchFinder::MatchResult &MR) { 764 | const DeclRefExpr *DRE = 765 | MR.Nodes.getNodeAs("tracevardeclrefexpr"); 766 | if (DRE) { 767 | if (DRE->getFoundDecl() == ND) { 768 | std::cout << DRE->getLocation().printToString(*(MR.SourceManager)) 769 | << "\n"; 770 | } 771 | } 772 | } 773 | 774 | void setND(NamedDecl const *Original_Declaration) { 775 | ND = Original_Declaration; 776 | } 777 | 778 | private: 779 | Rewriter &Rewrite [[maybe_unused]]; 780 | NamedDecl const *ND; 781 | }; 782 | /***********************************************************************************************/ 783 | class TraceVarHandler : public MatchFinder::MatchCallback { 784 | public: 785 | explicit TraceVarHandler(Rewriter &Rewrite) 786 | : Rewrite(Rewrite), SubMatcher(Rewrite) {} 787 | 788 | virtual void run(const MatchFinder::MatchResult &MR) override { 789 | const VarDecl *VD = MR.Nodes.getNodeAs("tracevar"); 790 | if (VD) { 791 | SourceRange SR = VD->getSourceRange(); 792 | SourceLocation SL = SR.getBegin(); 793 | CheckSLValidity(SL); 794 | if (Devi::IsTheMatchInSysHeader(CO_SYSHDR, MR, SL)) 795 | return void(); 796 | if (!Devi::IsTheMatchInMainFile(CO_MAINFILE, MR, SL)) 797 | return void(); 798 | auto NameRef = VD->getName(); 799 | if (CO_TRACE == NameRef.str()) { 800 | SubMatcher.setND(VD->getCanonicalDecl()->getUnderlyingDecl()); 801 | Matcher.addMatcher(declRefExpr(to(varDecl(hasName(VD->getName())))) 802 | .bind("tracevardeclrefexpr"), 803 | &SubMatcher); 804 | Matcher.matchAST(*(MR.Context)); 805 | } 806 | } 807 | } 808 | 809 | private: 810 | MatchFinder Matcher; 811 | Rewriter &Rewrite [[maybe_unused]]; 812 | TraceVarHandlerSub SubMatcher; 813 | }; 814 | /***********************************************************************************************/ 815 | class SubDynamicMatcher : public MatchFinder::MatchCallback { 816 | public: 817 | explicit SubDynamicMatcher(Rewriter &Rewrite) : Rewrite(Rewrite) {} 818 | 819 | virtual void run(const MatchFinder::MatchResult &MR) override {} 820 | 821 | private: 822 | MatchFinder Matcher; 823 | Rewriter &Rewrite [[maybe_unused]]; 824 | }; 825 | /***********************************************************************************************/ 826 | class DynamicMatcher : public MatchFinder::MatchCallback { 827 | public: 828 | explicit DynamicMatcher(Rewriter &Rewrite) 829 | : Rewrite(Rewrite), SubDynamicHandler(Rewrite) {} 830 | 831 | virtual void run(const MatchFinder::MatchResult &MR) override {} 832 | 833 | private: 834 | MatchFinder Matcher; 835 | Rewriter &Rewrite [[maybe_unused]]; 836 | SubDynamicMatcher SubDynamicHandler; 837 | }; 838 | /***********************************************************************************************/ 839 | class PPInclusion : public PPCallbacks { 840 | public: 841 | explicit PPInclusion(SourceManager *SM, Rewriter *Rewrite) 842 | : SM(*SM), Rewrite(*Rewrite) {} 843 | 844 | virtual bool FileNotFound(StringRef FileName, 845 | SmallVectorImpl &RecoveryPath) { 846 | std::cerr << CC_RED << "Header not found: " << FileName.str() << CC_NORMAL 847 | << "\n"; 848 | exit(1); 849 | } 850 | 851 | virtual void MacroDefined(const Token &MacroNameTok, 852 | const MacroDirective *MD) { 853 | if (CO_MACRO) { 854 | SourceLocation SL = MD->getLocation(); 855 | CheckSLValidity(SL); 856 | SL = Devi::SourceLocationHasMacro(SL, Rewrite, "start"); 857 | if (Devi::IsTheMatchInSysHeader(CO_SYSHDR, SM, SL)) 858 | return void(); 859 | if (!Devi::IsTheMatchInMainFile(CO_MAINFILE, SM, SL)) 860 | return void(); 861 | std::string name = MacroNameTok.getIdentifierInfo()->getName().str(); 862 | if (regex_handler(REGEX_PP(CO_REGEX), name)) { 863 | std::cout << name << "\t"; 864 | std::cout << SL.printToString(SM) << "\n"; 865 | } 866 | } 867 | } 868 | 869 | virtual void MacroExpands(const Token &MacroNameTok, 870 | const MacroDefinition &MD, SourceRange Range, 871 | const MacroArgs *Args) {} 872 | 873 | #if __clang_major__ <= 6 874 | virtual void InclusionDirective(SourceLocation HashLoc, 875 | const Token &IncludeTok, StringRef FileName, 876 | bool IsAngled, CharSourceRange FilenameRange, 877 | const FileEntry *File, StringRef SearchPath, 878 | StringRef RelativePath, 879 | const clang::Module *Imported) { 880 | #elif __clang_major__ >= 7 881 | virtual void InclusionDirective(SourceLocation HashLoc, 882 | const Token &IncludeTok, StringRef FileName, 883 | bool IsAngled, CharSourceRange FilenameRange, 884 | const FileEntry *File, StringRef SearchPath, 885 | StringRef RelativePath, 886 | const clang::Module *Imported, 887 | SrcMgr::CharacteristicKind FileType) { 888 | #endif 889 | if (CO_HEADER) { 890 | CheckSLValidity(HashLoc); 891 | SourceLocation SL = 892 | Devi::SourceLocationHasMacro(HashLoc, Rewrite, "start"); 893 | if (Devi::IsTheMatchInSysHeader(CO_SYSHDR, SM, SL)) { 894 | return void(); 895 | } 896 | if (!Devi::IsTheMatchInMainFile(CO_MAINFILE, SM, SL)) { 897 | return void(); 898 | } 899 | std::string name = FileName.str(); 900 | if (regex_handler(REGEX_PP(CO_REGEX), name)) { 901 | std::cout << name << "\t"; 902 | std::cout << SL.printToString(SM) << "\n"; 903 | } 904 | } 905 | } 906 | 907 | private: 908 | const SourceManager &SM [[maybe_unused]]; 909 | Rewriter &Rewrite [[maybe_unused]]; 910 | }; 911 | /***********************************************************************************************/ 912 | class CgrepDiagConsumer : public clang::DiagnosticConsumer { 913 | public: 914 | CgrepDiagConsumer() = default; 915 | virtual ~CgrepDiagConsumer() {} 916 | virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, 917 | const Diagnostic &Info) override { 918 | if ((clang::DiagnosticsEngine::Level::Error == DiagLevel) || 919 | (clang::DiagnosticsEngine::Level::Fatal == DiagLevel)) { 920 | SmallVector OutStr; 921 | Info.FormatDiagnostic(OutStr); 922 | std::cout << "Error:"; 923 | for (auto &iter : OutStr) 924 | std::cout << iter; 925 | ArrayRef SourceRanges = Info.getRanges(); 926 | for (auto &iter : SourceRanges) { 927 | if (Info.hasSourceManager()) { 928 | std::cout << iter.getBegin().printToString(Info.getSourceManager()) 929 | << ":" 930 | << iter.getEnd().printToString(Info.getSourceManager()) 931 | << "\n"; 932 | } 933 | } 934 | ArrayRef FixItHints [[maybe_unused]] = Info.getFixItHints(); 935 | std::cout << "\n"; 936 | } 937 | } 938 | }; 939 | /***********************************************************************************************/ 940 | class TraceASTConsumer : public ASTConsumer { 941 | public: 942 | explicit TraceASTConsumer(Rewriter &R) : HandlerForTraceVar(R) { 943 | Matcher.addMatcher(varDecl().bind("tracevar"), &HandlerForTraceVar); 944 | } 945 | 946 | void HandleTranslationUnit(ASTContext &Context) override { 947 | Matcher.matchAST(Context); 948 | } 949 | 950 | private: 951 | TraceVarHandler HandlerForTraceVar; 952 | MatchFinder Matcher; 953 | }; 954 | /***********************************************************************************************/ 955 | class DynamicASTConsumer : public ASTConsumer { 956 | public: 957 | explicit DynamicASTConsumer(Rewriter &R) : DynamicHandler(R) {} 958 | 959 | void HandleTranslationUnit(ASTContext &Context) override { 960 | Matcher.matchAST(Context); 961 | } 962 | 963 | private: 964 | DynamicMatcher DynamicHandler; 965 | MatchFinder Matcher; 966 | }; 967 | /***********************************************************************************************/ 968 | class CgrepASTConsumer : public ASTConsumer { 969 | public: 970 | explicit CgrepASTConsumer(Rewriter &R) 971 | : HandlerForVar(R), HandlerForClass(R), HandlerForCalledFunc(R), 972 | HandlerForCXXMethod(R), HandlerForField(R), HandlerForStruct(R), 973 | HandlerForUnion(R), HandlerForNamedDecl(R), HandlerForDeclRefExpr(R), 974 | HandlerForCallExpr(R), HandlerForCXXCallExpr(R), 975 | HandlerForRecordField(R), HandlerForRecord(R) { 976 | if (CO_FUNCTION || CO_ALL) { 977 | Matcher.addMatcher(functionDecl().bind("funcdecl"), 978 | &HandlerForCalledFunc); 979 | } 980 | if (CO_VAR || CO_ALL) { 981 | Matcher.addMatcher( 982 | varDecl(anyOf(unless(hasDescendant(expr(anything()))), 983 | hasDescendant(expr(anything()).bind("expr")))) 984 | .bind("vardecl"), 985 | &HandlerForVar); 986 | } 987 | if (CO_CLASS || CO_ALL) { 988 | // we are excluding the definitions here, since class declarations and 989 | // definitions will match separately, so for a class that is declared and 990 | // defined in the same location, we'll get two matches. A declaration can 991 | // happen without a definition but the other way around cannot be true. 992 | Matcher.addMatcher(recordDecl(allOf(isClass(), unless(isDefinition()))) 993 | .bind("classdecl"), 994 | &HandlerForClass); 995 | } 996 | if (CO_MEM_FUNCTION || CO_ALL) { 997 | Matcher.addMatcher(cxxMethodDecl().bind("cxxmethoddecl"), 998 | &HandlerForCXXMethod); 999 | } 1000 | if (CO_CFIELD || CO_ALL) { 1001 | Matcher.addMatcher(fieldDecl().bind("fielddecl"), &HandlerForField); 1002 | } 1003 | if (CO_STRUCT || CO_ALL) { 1004 | Matcher.addMatcher(recordDecl(allOf(isStruct(), unless(isDefinition()))) 1005 | .bind("structdecl"), 1006 | &HandlerForStruct); 1007 | } 1008 | if (CO_UNION || CO_ALL) { 1009 | Matcher.addMatcher(recordDecl(isUnion()).bind("uniondecl"), 1010 | &HandlerForUnion); 1011 | } 1012 | if (CO_NAMEDDECL) { 1013 | Matcher.addMatcher(namedDecl().bind("namedecl"), &HandlerForNamedDecl); 1014 | } 1015 | if (CO_DECLREFEXPR || CO_ALL) { 1016 | Matcher.addMatcher(declRefExpr().bind("declrefexpr"), 1017 | &HandlerForDeclRefExpr); 1018 | } 1019 | if (CO_CALL || CO_ALL) { 1020 | Matcher.addMatcher(callExpr().bind("callexpr"), &HandlerForCallExpr); 1021 | } 1022 | if (CO_CXXCALL || CO_ALL) { 1023 | Matcher.addMatcher(cxxMemberCallExpr().bind("cxxcallexpr"), 1024 | &HandlerForCXXCallExpr); 1025 | } 1026 | if (CO_CXXFIELD || CO_ALL) { 1027 | Matcher.addMatcher( 1028 | fieldDecl(hasParent(cxxRecordDecl())).bind("recordfielddecl"), 1029 | &HandlerForRecordField); 1030 | } 1031 | if (CO_RECORD || CO_ALL) { 1032 | Matcher.addMatcher(recordDecl().bind("recorddecl"), &HandlerForRecord); 1033 | } 1034 | } 1035 | 1036 | void HandleTranslationUnit(ASTContext &Context) override { 1037 | Matcher.matchAST(Context); 1038 | } 1039 | 1040 | private: 1041 | VDecl HandlerForVar; 1042 | ClassDecl HandlerForClass; 1043 | FunctionHandler HandlerForCalledFunc; 1044 | CXXMethodHandler HandlerForCXXMethod; 1045 | FieldHandler HandlerForField; 1046 | StructHandler HandlerForStruct; 1047 | UnionHandler HandlerForUnion; 1048 | NamedDeclHandler HandlerForNamedDecl; 1049 | DeclRefExprHandler HandlerForDeclRefExpr; 1050 | CallExprHandler HandlerForCallExpr; 1051 | CXXCallExprHandler HandlerForCXXCallExpr; 1052 | RecordFieldHandler HandlerForRecordField; 1053 | RecordHandler HandlerForRecord; 1054 | MatchFinder Matcher; 1055 | }; 1056 | /***********************************************************************************************/ 1057 | class TraceFrontendAction : public ASTFrontendAction { 1058 | public: 1059 | TraceFrontendAction() {} 1060 | ~TraceFrontendAction() {} 1061 | 1062 | std::unique_ptr CreateASTConsumer(CompilerInstance &CI, 1063 | StringRef file) override { 1064 | if (!CO_CLANGDIAG) { 1065 | DiagnosticsEngine &DE = CI.getPreprocessor().getDiagnostics(); 1066 | DE.setClient(BDCProto, false); 1067 | } 1068 | TheRewriter.setSourceMgr(CI.getSourceManager(), CI.getLangOpts()); 1069 | #if __clang_major__ <= 9 1070 | return llvm::make_unique(TheRewriter); 1071 | #endif 1072 | #if __clang_major__ >= 10 1073 | return std::make_unique(TheRewriter); 1074 | #endif 1075 | } 1076 | 1077 | private: 1078 | CgrepDiagConsumer *BDCProto = new CgrepDiagConsumer; 1079 | Rewriter TheRewriter; 1080 | }; 1081 | /***********************************************************************************************/ 1082 | class CgrepFrontendAction : public ASTFrontendAction { 1083 | public: 1084 | CgrepFrontendAction() {} 1085 | ~CgrepFrontendAction() { delete BDCProto; } 1086 | 1087 | void EndSourceFileAction() override { std::error_code EC; } 1088 | 1089 | std::unique_ptr CreateASTConsumer(CompilerInstance &CI, 1090 | StringRef file) override { 1091 | #if __clang_major__ <= 9 1092 | CI.getPreprocessor().addPPCallbacks( 1093 | llvm::make_unique(&CI.getSourceManager(), &TheRewriter)); 1094 | #endif 1095 | #if __clang_major__ >= 10 1096 | CI.getPreprocessor().addPPCallbacks( 1097 | std::make_unique(&CI.getSourceManager(), &TheRewriter)); 1098 | #endif 1099 | if (!CO_CLANGDIAG) { 1100 | DiagnosticsEngine &DE = CI.getPreprocessor().getDiagnostics(); 1101 | DE.setClient(BDCProto, false); 1102 | } 1103 | TheRewriter.setSourceMgr(CI.getSourceManager(), CI.getLangOpts()); 1104 | #if __clang_major__ <= 9 1105 | return llvm::make_unique(TheRewriter); 1106 | #endif 1107 | #if __clang_major__ >= 10 1108 | return std::make_unique(TheRewriter); 1109 | #endif 1110 | } 1111 | 1112 | private: 1113 | CgrepDiagConsumer *BDCProto = new CgrepDiagConsumer; 1114 | Rewriter TheRewriter; 1115 | }; 1116 | /***********************************************************************************************/ 1117 | /*Main*/ 1118 | int main(int argc, const char **argv) { 1119 | #if __clang_major__ >= 13 1120 | auto op = CommonOptionsParser::create(argc, argv, CGrepCat); 1121 | if (auto error = op.takeError()) { 1122 | errs() << toString(std::move(error)) << "\n"; 1123 | return 1; 1124 | } 1125 | ClangTool Tool(op->getCompilations(), op->getSourcePathList()); 1126 | #else 1127 | CommonOptionsParser op(argc, argv, CGrepCat); 1128 | ClangTool Tool(op.getCompilations(), op.getSourcePathList()); 1129 | #endif 1130 | int ret = 0; 1131 | 1132 | if ("" != CO_TRACE) { 1133 | ret = Tool.run(newFrontendActionFactory().get()); 1134 | } else { 1135 | ret = Tool.run(newFrontendActionFactory().get()); 1136 | } 1137 | 1138 | return ret; 1139 | } 1140 | /***********************************************************************************************/ 1141 | -------------------------------------------------------------------------------- /cgrep.roff: -------------------------------------------------------------------------------- 1 | 2 | .TH CGREP "29 Feb 2020" 3 | .SH Farzad Sadeghi 4 | cgrep \ - grep for C-family source files 5 | 6 | .SH NAME 7 | .PP 8 | cgrep 9 | 10 | .SH SYNOPSIS 11 | .PP 12 | cgrep [options] [target] 13 | 14 | .SH DESCRIPTION 15 | .PP 16 | \fBCgrep\fP [ OPTION ] [ TARGET ] 17 | .br 18 | Cgrep is a grep-like tool for the 19 | C-family languages. cgrep is written using clang's libtooling and as such 20 | will accept any option that clang accepts as well. Like clang, 21 | cgrep will require you to have a compilation database. 22 | .PP 23 | If you can build your sources without any specific command-line 24 | options, you can pass "--" as the last command-line option which 25 | tells clang to try to build the source without a compilation database. 26 | If you are using \fBmake\fP to build your code-base, you can use \fBBEAR(1)\fP 27 | to generate a compilation database. 28 | 29 | 30 | .SS Options 31 | .PP 32 | .TP 33 | \fB-A=\fP 34 | How many lines after the matched line to print. Defaults to 0. 35 | 36 | .TP 37 | \fB-B=\fP 38 | Howm many lines before the matched line to print. Defaults to 0. 39 | 40 | .TP 41 | \fB-C=\fP 42 | Howm many lines before and after the matched line to print. Defaults to 0. 43 | 44 | .TP 45 | \fB--all\fP 46 | Turns on all switches other than nameddecl. 47 | 48 | .TP 49 | \fB--awk\fP 50 | Outputs locations in a gawk friendly format, not meant for human consumption. Defaults to false. 51 | 52 | .TP 53 | \fB--call\fP 54 | Match function calls. 55 | 56 | .TP 57 | \fB--class\fP 58 | Match class declarations. 59 | 60 | .TP 61 | \fB--cxxcall\fP 62 | Matches member function calls. 63 | 64 | .TP 65 | \fB--cxxfield\fP 66 | Match CXX field declarations. 67 | 68 | .TP 69 | \fB--crecord\fP 70 | Match a record declarations. 71 | 72 | .TP 73 | \fB--declrefexpr\fP 74 | Matches declrefexpr. 75 | .br 76 | \fIdeclreefexpr\fPs are any instance of a declaration that is being reference. 77 | For example: 78 | .br 79 | uint_32 my_var; 80 | .br 81 | my_var = 10; 82 | .br 83 | In the second line, \fImy_var\fP is a declaration that is being referenced. 84 | The rule applies for all named declarations. 85 | 86 | .TP 87 | \fB--extra-arg=\fP 88 | Additional argument to append to the compiler command line. 89 | .br 90 | This is the option to use when you want to pass extra arguments 91 | to cgrep. These would be the clang command line options. 92 | 93 | .TP 94 | \fB--extra-arg-before=\fP 95 | Additional argument to prepend to the compiler command line. 96 | .br 97 | This is the option to use when you want to pass extra arguments 98 | to cgrep. These would be the clang command line options. 99 | 100 | .TP 101 | \fB--func=\fP 102 | Match function declarations. 103 | .br 104 | Function definitions are also function declarations so for a function 105 | that has a declaration and a definition, cgrep will find two results. 106 | 107 | .TP 108 | \fB--header\fP 109 | Match headers in header inclusions. 110 | 111 | .TP 112 | \fB--macro\fP 113 | Match macro definitions. 114 | 115 | .TP 116 | \fB--mainfile\fP 117 | Match identifiers in the main file only. Defaults to true. 118 | .br 119 | This option filters out matches found in the translation unit passed to 120 | cgrep. 121 | 122 | .TP 123 | \fB--memfunc\fP 124 | Match member function declarations. 125 | 126 | .TP 127 | \fB--cfield\fP 128 | Match C field declations. 129 | 130 | .TP 131 | \fB--clangdiag\fP 132 | Enables the normal diagnostics and fixits that you are used to see from clang. 133 | By defualt this option is set to false. cgrep will use its own dignosticConsumer 134 | when this option is set to false. 135 | 136 | .TP 137 | \fB--nameddecl\fP 138 | Matches all named declarations. 139 | 140 | .TP 141 | \fB--nocolor\fP 142 | The output will have no colors. 143 | .br 144 | The option is meant to be used for terminal emulators that don't support 145 | ANSI escape sequences. This option disables the printing of any escape 146 | sequences. 147 | 148 | .TP 149 | \fB--nodecl\fP 150 | Will not print out the declaration location for the matched identifier. 151 | 152 | .TP 153 | \fB--regex=\fP 154 | The regex to match against.One thing to keep in mind is that the regex will 155 | first pass through Cpp and then will pass through the regex engine. For 156 | example if you want to match for a literal '\' character, on the command 157 | line you will have to write '\\\\'. Cpp will remove the two backslashes 158 | deciding that they are both escape sequences and pass the regex engine 159 | the two backslashes, at which point the regex engine will understand 160 | the first backslash to be an escape sequence for the second backslash. 161 | 162 | .TP 163 | \fB--struct\fP 164 | Match structure declarations. 165 | 166 | .TP 167 | \fB--syshdr\fP 168 | Match identifiers in system header as well. Defaults to false. 169 | .br 170 | This option filters out headers that are included using <>. 171 | 172 | .TP 173 | \fB--union\fP 174 | Match union declarations. 175 | 176 | .TP 177 | \fB--var\fP 178 | Match variable declarations. 179 | .br 180 | This switch will also match function parameters. 181 | 182 | .SH EXAMPLE 183 | .PP 184 | .PP 185 | As an example if you want to match for declrefexpr and function calls, 186 | with no colors but with declarations present in the output you would write: 187 | .br 188 | cgrep --declrefexpr --call --nocolor --regex myawesomeregex myawesomecppfile.cpp 189 | .br 190 | In case you want to run cgrep without a compilation database, you can try: 191 | .br 192 | cgrep --declrefexpr --call --nocolor --regex myawesomeregex myawesomecppfile.cpp -- 193 | .PP 194 | Passing the clang "std" option will let cgrep know which language the source file 195 | is going to be in. For example: 196 | .br 197 | cgrep --declrefexpr --call --regex jaja --extra-arg=--std=c++17 myfile.cpp 198 | .br 199 | The above command lets cgrep know that the input file is a Cpp source file. 200 | .br 201 | To do the same for a C file, just pass it the C standard your source file 202 | is following: 203 | .br 204 | cgrep --declrefexpr --call --regex jaja --extra-arg=--std=c11 myfile.c 205 | 206 | .SH FILES 207 | .PP 208 | \fBcgrep\fP 209 | The cgrep executable. 210 | 211 | .SH "SEE ALSO" 212 | .PP 213 | BEAR(1) 214 | 215 | .SH BUGS 216 | .PP 217 | cgrep \fBwill not\fP print out warnings and errors that your code might have 218 | so please make sure clang can compile your code before attempting to use 219 | cgrep on your codebase. 220 | 221 | .SH COPYRIGHT 222 | .PP 223 | Copyright (C) by Farzad Sadeghi 224 | 225 | 226 | .SH "AUTHORS" 227 | Farzad Sadeghi 228 | -------------------------------------------------------------------------------- /covrun.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/sh 2 | 3 | LLVM_PROFILE_FILE="one.profraw" "./cgrep-cov" -A 1 -B 1 --func --var --regex n[aA]m ./cgrep.cpp 4 | LLVM_PROFILE_FILE="one.profraw" "./cgrep-cov" -A 1 -B 1 --func --var --awk --regex n[aA]m ./cgrep.cpp 5 | LLVM_PROFILE_FILE="two.profraw" "./cgrep-cov" -A 1 -B 1 --func --declrefexpr --regex n[aA]m --nocolor ./cgrep.cpp 6 | LLVM_PROFILE_FILE="three.profraw" "./cgrep-cov" -A 1 -B 1 --func --declrefexpr --memfunc --call --cxxcall --var --regex run ./cgrep.cpp 7 | LLVM_PROFILE_FILE="four.profraw" "./cgrep-cov" -A 1 -B 1 --macro --header --regex n[aA]m ./cgrep.cpp 8 | LLVM_PROFILE_FILE="five.profraw" "./cgrep-cov" -A 1 -B 1 --class --regex and ./cgrep.cpp 9 | LLVM_PROFILE_FILE="six.profraw" "./cgrep-cov" -A 1 -B 1 --struct --union --regex n[aA]m ./cgrep.cpp 10 | LLVM_PROFILE_FILE="seven.profraw" "./cgrep-cov" -A 1 -B 1 --nameddecl --regex n[aA]m ./cgrep.cpp 11 | LLVM_PROFILE_FILE="eight.profraw" "./cgrep-cov" -A 1 -B 1 --cxxcall --call --regex add ./cgrep.cpp 12 | LLVM_PROFILE_FILE="nine.profraw" "./cgrep-cov" -A 1 -B 1 --cfield --regex ite ./cgrep.cpp 13 | LLVM_PROFILE_FILE="ten.profraw" "./cgrep-cov" --union --regex [Uu]nion ./test/main.cpp 14 | LLVM_PROFILE_FILE="eleven.profraw" "./cgrep-cov" --struct --regex [sS]truct ./test/main.cpp 15 | LLVM_PROFILE_FILE="twelve.profraw" "./cgrep-cov" --dir ./ --regex run --func ./cgrep.cpp 16 | -------------------------------------------------------------------------------- /docker/11/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM debian:bullseye-slim 2 | 3 | RUN apt update && apt upgrade -y 4 | RUN apt install -y wget cmake git lsb-release software-properties-common gnupg2 5 | RUN wget apt.llvm.org/llvm.sh && chmod +x ./llvm.sh && ./llvm.sh 11 6 | RUN apt install -y llvm-11-dev libclang-common-11-dev libclang-11-dev clang-11 7 | 8 | RUN git clone https://github.com/bloodstalker/cgrep \ 9 | && cd cgrep \ 10 | && git submodule init \ 11 | && git submodule update \ 12 | && mkdir build \ 13 | && cd build \ 14 | && cmake ../ -DLLVM_CONF=llvm-config-11 -DCMAKE_CXX_COMPILER=clang++-11 -DUSE_MONOLITH_LIBTOOLING=OFF\ 15 | && make 16 | 17 | RUN rm -rf /var/apt/cache 18 | -------------------------------------------------------------------------------- /docker/12/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM debian:bullseye-slim 2 | 3 | RUN apt update && apt upgrade -y 4 | RUN apt install -y wget cmake git lsb-release software-properties-common gnupg2 5 | RUN wget apt.llvm.org/llvm.sh && chmod +x ./llvm.sh && ./llvm.sh 12 6 | RUN apt install -y llvm-12-dev libclang-common-12-dev libclang-12-dev clang-12 7 | 8 | RUN git clone https://github.com/bloodstalker/cgrep \ 9 | && cd cgrep \ 10 | && git submodule init \ 11 | && git submodule update \ 12 | && mkdir build \ 13 | && cd build \ 14 | && cmake ../ -DLLVM_CONF=llvm-config-12 -DCMAKE_CXX_COMPILER=clang++-12 -DUSE_MONOLITH_LIBTOOLING=OFF\ 15 | && make 16 | 17 | RUN rm -rf /var/apt/cache 18 | -------------------------------------------------------------------------------- /docker/13/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM debian:bullseye-slim 2 | 3 | RUN apt update && apt upgrade -y 4 | RUN apt install -y wget cmake git lsb-release software-properties-common gnupg2 5 | RUN wget apt.llvm.org/llvm.sh && chmod +x ./llvm.sh && ./llvm.sh 13 6 | RUN apt install -y llvm-13-dev libclang-common-13-dev libclang-13-dev clang-13 7 | 8 | RUN git clone https://github.com/bloodstalker/cgrep \ 9 | && cd cgrep \ 10 | && git submodule init \ 11 | && git submodule update \ 12 | && mkdir build \ 13 | && cd build \ 14 | && cmake ../ -DLLVM_CONF=llvm-config-13 -DCMAKE_CXX_COMPILER=clang++-13 -DUSE_MONOLITH_LIBTOOLING=OFF\ 15 | && make 16 | 17 | RUN rm -rf /var/apt/cache 18 | -------------------------------------------------------------------------------- /docker/14/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM debian:bullseye-slim 2 | 3 | RUN apt update && apt upgrade -y 4 | RUN apt install -y wget cmake git lsb-release software-properties-common gnupg2 5 | RUN wget apt.llvm.org/llvm.sh && chmod +x ./llvm.sh && ./llvm.sh 14 6 | RUN apt install -y llvm-14-dev libclang-common-14-dev libclang-14-dev clang-14 7 | 8 | RUN git clone https://github.com/bloodstalker/cgrep \ 9 | && cd cgrep \ 10 | && git submodule init \ 11 | && git submodule update \ 12 | && mkdir build \ 13 | && cd build \ 14 | && cmake ../ -DLLVM_CONF=llvm-config-14 -DCMAKE_CXX_COMPILER=clang++-14 -DUSE_MONOLITH_LIBTOOLING=OFF\ 15 | && make 16 | 17 | RUN rm -rf /var/apt/cache 18 | -------------------------------------------------------------------------------- /docker/15/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM debian:bullseye-slim 2 | 3 | RUN apt update && apt upgrade -y 4 | RUN apt install -y wget cmake git lsb-release software-properties-common gnupg2 5 | RUN wget apt.llvm.org/llvm.sh && chmod +x ./llvm.sh && ./llvm.sh 15 6 | RUN apt install -y llvm-15-dev libclang-common-15-dev libclang-15-dev clang-15 libclang-cpp15-dev 7 | 8 | RUN git clone https://github.com/bloodstalker/cgrep \ 9 | && cd cgrep \ 10 | && git submodule init \ 11 | && git submodule update \ 12 | && mkdir build \ 13 | && cd build \ 14 | && cmake ../ -DLLVM_CONF=llvm-config-15 -DCMAKE_CXX_COMPILER=clang++-15 -DUSE_MONOLITH_LIBTOOLING=ON\ 15 | && make 16 | 17 | RUN rm -rf /var/apt/cache 18 | -------------------------------------------------------------------------------- /docker/16/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM debian:bullseye-slim 2 | 3 | RUN apt update && apt upgrade -y 4 | RUN apt install -y wget cmake git lsb-release software-properties-common gnupg2 5 | RUN wget https://apt.llvm.org/llvm.sh && chmod +x ./llvm.sh && ./llvm.sh 16 6 | RUN apt install -y llvm-16-dev libclang-common-16-dev libclang-16-dev clang-16 libclang-cpp16-dev 7 | 8 | RUN git clone https://github.com/bloodstalker/cgrep \ 9 | && cd cgrep \ 10 | && git submodule init \ 11 | && git submodule update \ 12 | && mkdir build \ 13 | && cd build \ 14 | && cmake ../ -DLLVM_CONF=llvm-config-16 -DCMAKE_CXX_COMPILER=clang++-16 -DUSE_MONOLITH_LIBTOOLING=ON\ 15 | && make 16 | 17 | RUN rm -rf /var/apt/cache 18 | -------------------------------------------------------------------------------- /docker/17/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM debian:bullseye-slim 2 | 3 | RUN apt update && apt upgrade -y 4 | RUN apt install -y wget cmake git lsb-release software-properties-common gpg 5 | RUN wget https://apt.llvm.org/llvm.sh && chmod +x ./llvm.sh && ./llvm.sh 17 6 | RUN apt install -y llvm-17-dev libclang-common-17-dev libclang-17-dev clang-17 libclang-cpp17-dev 7 | 8 | RUN git clone https://github.com/bloodstalker/cgrep \ 9 | && cd cgrep \ 10 | && git submodule init \ 11 | && git submodule update \ 12 | && mkdir build \ 13 | && cd build \ 14 | && cmake ../ -DLLVM_CONF=llvm-config-17 -DCMAKE_CXX_COMPILER=clang++-17 -DUSE_MONOLITH_LIBTOOLING=ON\ 15 | && make 16 | 17 | RUN rm -rf /var/apt/cache 18 | -------------------------------------------------------------------------------- /docker/18/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM debian:bullseye-slim 2 | 3 | RUN apt update && apt upgrade -y 4 | RUN apt install -y wget cmake git lsb-release software-properties-common gnupg2 5 | RUN wget https://apt.llvm.org/llvm.sh && chmod +x ./llvm.sh && ./llvm.sh 18 6 | RUN apt install -y llvm-18-dev libclang-common-18-dev libclang-18-dev clang-18 libclang-cpp18-dev 7 | 8 | RUN git clone https://github.com/bloodstalker/cgrep \ 9 | && cd cgrep \ 10 | && git submodule init \ 11 | && git submodule update \ 12 | && mkdir build \ 13 | && cd build \ 14 | && cmake ../ -DLLVM_CONF=llvm-config-18 -DCMAKE_CXX_COMPILER=clang++-18 -DUSE_MONOLITH_LIBTOOLING=ON\ 15 | && make 16 | 17 | RUN rm -rf /var/apt/cache 18 | -------------------------------------------------------------------------------- /docker/arch/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM archlinux:base-devel-20240908.0.261281 2 | 3 | RUN pacman -Syu --noconfirm 4 | RUN pacman -S --noconfirm wget cmake git gnupg llvm-libs llvm openmp clang 5 | 6 | RUN git clone https://github.com/bloodstalker/cgrep \ 7 | && cd cgrep \ 8 | && git submodule init \ 9 | && git submodule update \ 10 | && mkdir build \ 11 | && cd build \ 12 | && cmake ../ -DLLVM_CONF=llvm-config -DCMAKE_CXX_COMPILER=clang++ -DUSE_MONOLITH_LIBTOOLING=ON\ 13 | && make 14 | 15 | RUN pacman -Scc 16 | -------------------------------------------------------------------------------- /makefile: -------------------------------------------------------------------------------- 1 | TARGET=cgrep 2 | SHELL=sh 3 | SHELL?=sh 4 | CC=clang 5 | CC?=clang 6 | ifdef OS 7 | CFLAGS=-std=c11 8 | else 9 | CFLAGS=-fpic -std=c11 10 | endif 11 | CXX=clang++ 12 | CXX?=clang++ 13 | ifdef OS 14 | CXX_FLAGS= 15 | else 16 | CXX_FLAGS=-fpic 17 | endif 18 | CXX_EXTRA?= 19 | CTAGS_I_PATH?=./ 20 | #LD_FLAGS= -lstdc++fs 21 | LD_FLAGS= 22 | EXTRA_LD_FLAGS?= 23 | ADD_SANITIZERS_CC= -g -fsanitize=address -fno-omit-frame-pointer 24 | ADD_SANITIZERS_LD= -g -fsanitize=address 25 | MEM_SANITIZERS_CC= -g -fsanitize=memory -fno-omit-frame-pointer 26 | MEM_SANITIZERS_LD= -g -fsanitize=memory 27 | UB_SANITIZERS_CC= -g -fsanitize=undefined -fno-omit-frame-pointer 28 | UB_SANITIZERS_LD= -g -fsanitize=undefined 29 | COV_CXX= -fprofile-instr-generate -fcoverage-mapping 30 | COV_LD= -fprofile-instr-generate 31 | # BUILD_MODES are=RELEASE(default), DEBUG,ADDSAN,MEMSAN,UBSAN 32 | BUILD_MODE?=RELEASE 33 | OBJ_LIST:=$(patsubst %.cpp, %.o, $(wildcard *.cpp)) 34 | ASM_LIST:=$(patsubst %.cpp, %.dis, $(wildcard *.cpp)) 35 | 36 | LLVM_CONF?=llvm-config 37 | LLVM_CXX_FLAGS=$(shell $(LLVM_CONF) --cxxflags) 38 | LLVM_CXX_FLAGS+=-I$(shell $(LLVM_CONF) --src-root)/tools/clang/include\ 39 | -I$(shell $(LLVM_CONF) --obj-root)/tools/clang/include\ 40 | -std=c++17 -fexceptions 41 | LLVM_LD_FLAGS=-Wl,--start-group -lclangAST -lclangAnalysis -lclangBasic\ 42 | -lclangDriver -lclangEdit -lclangFrontend -lclangFrontendTool\ 43 | -lclangLex -lclangParse -lclangSema -lclangEdit -lclangASTMatchers\ 44 | -lclangRewrite -lclangRewriteFrontend -lclangStaticAnalyzerFrontend\ 45 | -lclangStaticAnalyzerCheckers -lclangStaticAnalyzerCore\ 46 | -lclangSerialization -lclangToolingCore -lclangTooling -lstdc++\ 47 | -lLLVMRuntimeDyld -lm -Wl,--end-group 48 | LLVM_LD_FLAGS+=$(shell $(LLVM_CONF) --ldflags --libs --system-libs) 49 | 50 | CXX_FLAGS+=$(LLVM_CXX_FLAGS) 51 | LD_FLAGS+=$(LLVM_LD_FLAGS) 52 | 53 | MAKEFLAGS+=--warn-undefined-variables 54 | ifeq ($(BUILD_MODE), ADDSAN) 55 | ifeq ($(CXX), g++) 56 | $(error This build mode is only useable with clang++.) 57 | endif 58 | CXX_EXTRA+=$(ADD_SANITIZERS_CC) 59 | EXTRA_LD_FLAGS+=$(ADD_SANITIZERS_LD) 60 | endif 61 | 62 | ifeq ($(BUILD_MODE), MEMSAN) 63 | ifeq ($(CXX), g++) 64 | $(error This build mode is only useable with clang++.) 65 | endif 66 | CXX_EXTRA+=$(MEM_SANITIZERS_CC) 67 | EXTRA_LD_FLAGS+=$(MEM_SANITIZERS_LD) 68 | endif 69 | 70 | ifeq ($(BUILD_MODE), UBSAN) 71 | ifeq ($(CXX), g++) 72 | $(error This build mode is only useable with clang++.) 73 | endif 74 | CXX_EXTRA+=$(UB_SANITIZERS_CC) 75 | EXTRA_LD_FLAGS+=$(UB_SANITIZERS_LD) 76 | endif 77 | 78 | SRCS:=$(wildcard *.cpp) 79 | HDRS:=$(wildcard *.hpp) 80 | CXX_FLAGS+=$(CXX_EXTRA) 81 | LD_FLAGS+=$(EXTRA_LD_FLAGS) 82 | 83 | .DEFAULT:all 84 | 85 | .PHONY:all clean help ASM SO TAGS 86 | 87 | all: $(TARGET) 88 | 89 | everything:$(TARGET) A ASM SO $(TARGET)-dbg TAGS $(TARGET)-cov 90 | 91 | depend:.depend 92 | 93 | .depend:$(SRCS) 94 | rm -rf .depend 95 | $(CXX) -MM $(CXX_FLAGS) $^ > ./.depend 96 | echo $(patsubst %.o:, %.odbg:, $(shell $(CXX) -MM $(CXX_FLAGS) $^)) | sed -r 's/[A-Za-z0-9\-\_]+\.odbg/\n&/g' >> ./.depend 97 | echo $(patsubst %.o:, %.ocov:, $(shell $(CXX) -MM $(CXX_FLAGS) $^)) | sed -r 's/[A-Za-z0-9\-\_]+\.ocov/\n&/g' >> ./.depend 98 | 99 | -include ./.depend 100 | 101 | %.o:%.cpp 102 | $(CXX) $(CXX_FLAGS) -c $< -o $@ 103 | 104 | %.odbg:%.cpp 105 | $(CXX) $(CXX_FLAGS) -g -c $< -o $@ 106 | 107 | %.ocov:%.cpp 108 | $(CXX) $(CXX_FLAGS) $(COV_CXX) -c $< -o $@ 109 | 110 | ./cfe-extra/cfe_extra.o:./cfe-extra/cfe_extra.cpp 111 | $(CXX) $(CXX_FLAGS) -c $< -o $@ 112 | 113 | ./cfe-extra/cfe_extra.odbg:./cfe-extra/cfe_extra.cpp 114 | $(CXX) $(CXX_FLAGS) -g -c $< -o $@ 115 | 116 | ./cfe-extra/cfe_extra.ocov:./cfe-extra/cfe_extra.cpp 117 | $(CXX) $(CXX_FLAGS) $(COV_CXX) -c $< -o $@ 118 | 119 | $(TARGET): $(TARGET).o ./cfe-extra/cfe_extra.o 120 | $(CXX) $^ $(LD_FLAGS) -o $@ 121 | 122 | $(TARGET)-static: $(TARGET).o ./cfe-extra/cfe_extra.o 123 | $(CXX) $^ $(LD_FLAGS) -static -o $@ 124 | 125 | $(TARGET)-dbg: $(TARGET).odbg ./cfe-extra/cfe_extra.odbg 126 | $(CXX) $^ $(LD_FLAGS) -g -o $@ 127 | 128 | $(TARGET)-cov: $(TARGET).ocov ./cfe-extra/cfe_extra.ocov 129 | $(CXX) $^ $(LD_FLAGS) $(COV_LD) -o $@ 130 | 131 | cov: runcov 132 | @llvm-profdata merge -sparse ./one.profraw ./two.profraw ./three.profraw ./four.profraw ./five.profraw ./six.profraw ./seven.profraw \ 133 | ./eight.profraw ./nine.profraw ./ten.profraw ./eleven.profraw ./twelve.profraw -o ./cgrep.profdata 134 | @llvm-cov show $(TARGET)-cov -instr-profile=cgrep.profdata -ignore-filename-regex=llvm clang 135 | 136 | covrep: runcov 137 | @llvm-profdata merge -sparse ./one.profraw ./two.profraw ./three.profraw ./four.profraw ./five.profraw ./six.profraw ./seven.profraw \ 138 | ./eight.profraw ./nine.profraw ./ten.profraw ./eleven.profraw ./twelve.profraw -o ./cgrep.profdata 139 | @llvm-cov report $(TARGET)-cov -instr-profile=cgrep.profdata -ignore-filename-regex=llvm -ignore-filename-regex=clang -show-functions cgrep.cpp 140 | 141 | ASM:$(ASM_LIST) 142 | 143 | SO:$(TARGET).so 144 | 145 | A:$(TARGET).a 146 | 147 | TAGS:tags 148 | 149 | tags:$(SRCS) 150 | $(shell $(CXX) -c $(shell $(LLVM_CONF) --cxxflags) -I$(shell $(LLVM_CONF) --src-root)/tools/clang/include -I$(shell $(LLVM_CONF) --obj-root)/tools/clang/include -I $(CTAGS_I_PATH) -M $(SRCS)|\ 151 | sed -e 's/[\\ ]/\n/g'|sed -e '/^$$/d' -e '/\.o:[ \t]*$$/d'|\ 152 | ctags -L - --c++-kinds=+p --fields=+iaS --extra=+q) 153 | 154 | %.dis: %.o 155 | objdump -r -d -M intel -S $< > $@ 156 | 157 | $(TARGET).so: $(TARGET).o ./cfe-extra/cfe_extra.o 158 | $(CXX) $^ $(LD_FLAGS) -shared -o $@ 159 | 160 | $(TARGET).a: $(TARGET).o ./cfe-extra/cfe_extra.o 161 | ar rcs $(TARGET).a $(TARGET).o 162 | 163 | runcov: $(TARGET)-cov 164 | - ./covrun.sh 165 | 166 | test: $(TARGET) 167 | $(TARGET) --A 1 --B 1 --regex write --memvar ./cgrep.cpp 168 | 169 | valgrind: $(TARGET) 170 | - valgrind --track-origins=yes --leak-check=full --show-leak-kinds=all $(TARGET) $(TARGET).cpp -- 171 | 172 | format: 173 | - clang-format -i $(SRCS) $(HDRS) 174 | 175 | install: $(TARGET) 176 | ln -s ./$(TARGET) /usr/local/bin/ 177 | 178 | clean: 179 | rm -f *.o *.dis *.odbg *.ocov *~ $(TARGET) $(TARGET).so $(TARGET)-static $(TARGET)-dbg $(TARGET).a $(TARGET)-cov 180 | 181 | deepclean: clean 182 | - rm tags 183 | - rm .depend 184 | - $(MAKE) -C ./cfe-extra clean 185 | - rm *.gch 186 | 187 | help: 188 | @echo "--all is the default target, runs $(TARGET) target" 189 | @echo "--everything will build everything" 190 | @echo "--SO will generate the so" 191 | @echo "--ASM will generate assembly files" 192 | @echo "--TAGS will generate tags file" 193 | @echo "--$(TARGET) builds the dynamically-linked executable" 194 | @echo "--$(TARGET)-dbg will generate the debug build. BUILD_MODE should be set to DEBUG to work" 195 | @echo "--$(TARGET)-static will statically link the executable to the libraries" 196 | @echo "--$(TARGET)-cov is the coverage build" 197 | @echo "--cov will print the line coverage report" 198 | @echo "--covrep will print the coverage report" 199 | @echo "--A will build the static library" 200 | @echo "--TAGS will build the tags file" 201 | @echo "--clean" 202 | @echo "--deepclean will clean almost everything" 203 | -------------------------------------------------------------------------------- /makeman.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/sh 2 | 3 | groff -man -Tascii ./cgrep.roff 4 | -------------------------------------------------------------------------------- /pch.hpp: -------------------------------------------------------------------------------- 1 | #include "./cfe-extra/cfe_extra.h" 2 | #include "clang/AST/AST.h" 3 | #include "clang/AST/ASTConsumer.h" 4 | #include "clang/ASTMatchers/ASTMatchFinder.h" 5 | #include "clang/ASTMatchers/ASTMatchers.h" 6 | #include "clang/Basic/LLVM.h" 7 | #include "clang/Frontend/CompilerInstance.h" 8 | #include "clang/Frontend/FrontendActions.h" 9 | #include "clang/Lex/Lexer.h" 10 | #include "clang/Rewrite/Core/Rewriter.h" 11 | #include "clang/Tooling/CommonOptionsParser.h" 12 | #include "clang/Tooling/Tooling.h" 13 | #include "llvm/Support/raw_ostream.h" 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | -------------------------------------------------------------------------------- /run.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/sh 2 | 3 | "./cgrep" -A 1 -B 1 --func --var --regex n[aA]m ./cgrep.cpp 4 | -------------------------------------------------------------------------------- /test/callexpr.cpp: -------------------------------------------------------------------------------- 1 | 2 | namespace callexpr_ns{ 3 | int testFunction(int a, int b) { return a + b; } 4 | 5 | class testClass { 6 | public: 7 | testClass() = default; 8 | int testMemberFunction(int a, int b) { return a + b; } 9 | 10 | private: 11 | /* data */ 12 | }; 13 | 14 | struct testStruct { 15 | public: 16 | testStruct() = default; 17 | int a; 18 | int b; 19 | char *c; 20 | int testMemberFunctionn(int a, int b) { return a + b; } 21 | }; 22 | } // namespace 23 | 24 | int __attribute__((weak)) main(int argc, char *argv[]) { 25 | int a = 10; 26 | int b = 10; 27 | callexpr_ns::testFunction(a, b); 28 | callexpr_ns::testClass tc; 29 | tc.testMemberFunction(a, b); 30 | callexpr_ns::testStruct ts; 31 | ts.testMemberFunctionn(a, b); 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /test/classdecl.cpp: -------------------------------------------------------------------------------- 1 | 2 | namespace classdecl_ns{ 3 | class testClass { 4 | public: 5 | testClass(); 6 | virtual ~testClass(); 7 | 8 | private: 9 | }; 10 | 11 | #define classdeclmacro classDeclMacroExpanded 12 | class anotherTestClass { 13 | public: 14 | anotherTestClass(); 15 | virtual ~anotherTestClass(); 16 | 17 | private: 18 | }; 19 | }; // namespace 20 | -------------------------------------------------------------------------------- /test/compile_commands.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "command": "c++ -c -std=c++11 -fpic -o fielddecl.o fielddecl.cpp", 4 | "directory": "/home/bloodstalker/extra/cgrep/test", 5 | "file": "/home/bloodstalker/extra/cgrep/test/fielddecl.cpp" 6 | }, 7 | { 8 | "command": "c++ -c -std=c++11 -fpic -o cxxmethoddecl.o cxxmethoddecl.cpp", 9 | "directory": "/home/bloodstalker/extra/cgrep/test", 10 | "file": "/home/bloodstalker/extra/cgrep/test/cxxmethoddecl.cpp" 11 | }, 12 | { 13 | "command": "c++ -c -std=c++11 -fpic -o vardecl.o vardecl.cpp", 14 | "directory": "/home/bloodstalker/extra/cgrep/test", 15 | "file": "/home/bloodstalker/extra/cgrep/test/vardecl.cpp" 16 | }, 17 | { 18 | "command": "c++ -c -std=c++11 -fpic -o cxxmembercallexpr.o cxxmembercallexpr.cpp", 19 | "directory": "/home/bloodstalker/extra/cgrep/test", 20 | "file": "/home/bloodstalker/extra/cgrep/test/cxxmembercallexpr.cpp" 21 | }, 22 | { 23 | "command": "c++ -c -std=c++11 -fpic -o nameddecldef.o nameddecldef.cpp", 24 | "directory": "/home/bloodstalker/extra/cgrep/test", 25 | "file": "/home/bloodstalker/extra/cgrep/test/nameddecldef.cpp" 26 | }, 27 | { 28 | "command": "c++ -c -std=c++11 -fpic -o cxxrecorddecl.o cxxrecorddecl.cpp", 29 | "directory": "/home/bloodstalker/extra/cgrep/test", 30 | "file": "/home/bloodstalker/extra/cgrep/test/cxxrecorddecl.cpp" 31 | }, 32 | { 33 | "command": "c++ -c -std=c++11 -fpic -o structdecl.o structdecl.cpp", 34 | "directory": "/home/bloodstalker/extra/cgrep/test", 35 | "file": "/home/bloodstalker/extra/cgrep/test/structdecl.cpp" 36 | }, 37 | { 38 | "command": "c++ -c -std=c++11 -fpic -o callexpr.o callexpr.cpp", 39 | "directory": "/home/bloodstalker/extra/cgrep/test", 40 | "file": "/home/bloodstalker/extra/cgrep/test/callexpr.cpp" 41 | }, 42 | { 43 | "command": "c++ -c -std=c++11 -fpic -o uniondecdef.o uniondecdef.cpp", 44 | "directory": "/home/bloodstalker/extra/cgrep/test", 45 | "file": "/home/bloodstalker/extra/cgrep/test/uniondecdef.cpp" 46 | }, 47 | { 48 | "command": "c++ -c -std=c++11 -fpic -o declrefexpr.o declrefexpr.cpp", 49 | "directory": "/home/bloodstalker/extra/cgrep/test", 50 | "file": "/home/bloodstalker/extra/cgrep/test/declrefexpr.cpp" 51 | }, 52 | { 53 | "command": "c++ -c -std=c++11 -fpic -o function.o function.cpp", 54 | "directory": "/home/bloodstalker/extra/cgrep/test", 55 | "file": "/home/bloodstalker/extra/cgrep/test/function.cpp" 56 | }, 57 | { 58 | "command": "c++ -c -std=c++11 -fpic -o main.o main.cpp", 59 | "directory": "/home/bloodstalker/extra/cgrep/test", 60 | "file": "/home/bloodstalker/extra/cgrep/test/main.cpp" 61 | }, 62 | { 63 | "command": "c++ -c -std=c++11 -fpic -o classdecl.o classdecl.cpp", 64 | "directory": "/home/bloodstalker/extra/cgrep/test", 65 | "file": "/home/bloodstalker/extra/cgrep/test/classdecl.cpp" 66 | } 67 | ] -------------------------------------------------------------------------------- /test/cxxmembercallexpr.cpp: -------------------------------------------------------------------------------- 1 | 2 | namespace cxxmembercallexpr_ns{ 3 | class testClass { 4 | public: 5 | testClass() = default; 6 | int testFunction(int a, int b) { return a + b; } 7 | 8 | private: 9 | }; 10 | 11 | struct testStruct { 12 | public: 13 | testStruct() = default; 14 | int testFunction(int a, int b) { 15 | return a + b; 16 | }; 17 | }; 18 | } // namespace 19 | 20 | int __attribute__((weak)) main(int argc, char *argv[]) { 21 | int a = 10; 22 | int b = 10; 23 | cxxmembercallexpr_ns::testClass tc; 24 | tc.testFunction(a, b); 25 | cxxmembercallexpr_ns::testStruct ts; 26 | ts.testFunction(a, b); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /test/cxxmethoddecl.cpp: -------------------------------------------------------------------------------- 1 | 2 | namespace cxxmethoddecl_ns{ 3 | #define cxxmethoddeclmacro cxxMethodTwo 4 | class testClass { 5 | public: 6 | testClass(); 7 | virtual ~testClass(); 8 | 9 | void cxxMethodOne(void); 10 | void cxxMethodOne(int a); 11 | void cxxmethoddeclmacro(void); 12 | 13 | private: 14 | }; 15 | }; // namespace 16 | -------------------------------------------------------------------------------- /test/cxxrecorddecl.cpp: -------------------------------------------------------------------------------- 1 | 2 | namespace cxxrecorddecl_ns{ 3 | class testClass { 4 | public: 5 | testClass(); 6 | virtual ~testClass(); 7 | 8 | private: 9 | /* data */ 10 | }; 11 | 12 | struct testStruct { 13 | /* data */ 14 | }; 15 | }; // namespace 16 | -------------------------------------------------------------------------------- /test/declrefexpr.cpp: -------------------------------------------------------------------------------- 1 | 2 | namespace declrefexpr_ns{ 3 | 4 | class testClass { 5 | public: 6 | testClass() = default; 7 | int testa; 8 | 9 | private: 10 | }; 11 | } // namespace 12 | 13 | int __attribute__((weak)) main(int argc, char *argv[]) { 14 | int a; 15 | int testb; 16 | declrefexpr_ns::testClass tc; 17 | tc.testa = 10; 18 | 19 | return a + testb; 20 | } 21 | -------------------------------------------------------------------------------- /test/fielddecl.cpp: -------------------------------------------------------------------------------- 1 | 2 | namespace fielddecl_ns{ 3 | #define fieldmacro fieldthree 4 | 5 | struct testStruct { 6 | int fieldone; 7 | float testfieldtwo; 8 | int fieldmacro; 9 | }; 10 | 11 | union testUnion { 12 | int testfieldone; 13 | bool fieldtwo; 14 | }; 15 | 16 | class testClass { 17 | public: 18 | testClass(void); 19 | virtual ~testClass(); 20 | 21 | void testMethod(void); 22 | 23 | private: 24 | int an_arg; 25 | int another_arg; 26 | }; 27 | } // namespace 28 | -------------------------------------------------------------------------------- /test/function.cpp: -------------------------------------------------------------------------------- 1 | 2 | namespace function_ns{ 3 | void testMyFunc(void) { return; } 4 | 5 | class yolo { 6 | public: 7 | yolo() = default; 8 | yolo(int a) : a(a) {} 9 | virtual ~yolo(); 10 | 11 | void testyolofunc(void) { return; } 12 | 13 | private: 14 | int a; 15 | }; 16 | 17 | #define MFunc macroedFunc 18 | 19 | void testFunc(void) { return; } 20 | } // namespace 21 | -------------------------------------------------------------------------------- /test/main.ast: -------------------------------------------------------------------------------- 1 | TranslationUnitDecl 0x7e63838 <>  2 | |-TypedefDecl 0x7e64110 <>  implicit __int128_t '__int128' 3 | | `-BuiltinType 0x7e63dd0 '__int128' 4 | |-TypedefDecl 0x7e64180 <>  implicit __uint128_t 'unsigned __int128' 5 | | `-BuiltinType 0x7e63df0 'unsigned __int128' 6 | |-TypedefDecl 0x7e644f8 <>  implicit __NSConstantString '__NSConstantString_tag' 7 | | `-RecordType 0x7e64270 '__NSConstantString_tag' 8 | | `-CXXRecord 0x7e641d8 '__NSConstantString_tag' 9 | |-TypedefDecl 0x7e64590 <>  implicit __builtin_ms_va_list 'char *' 10 | | `-PointerType 0x7e64550 'char *' 11 | | `-BuiltinType 0x7e638d0 'char' 12 | |-TypedefDecl 0x7ea1338 <>  implicit __builtin_va_list '__va_list_tag [1]' 13 | | `-ConstantArrayType 0x7ea12e0 '__va_list_tag [1]' 1 14 | | `-RecordType 0x7e64680 '__va_list_tag' 15 | | `-CXXRecord 0x7e645e8 '__va_list_tag' 16 | |-CXXRecordDecl 0x7ea1390 <main.cpp:2:1, line:12:1> line:2:7 referenced class myClass definition 17 | | |-DefinitionData standard_layout has_user_declared_ctor 18 | | | |-DefaultConstructor exists trivial 19 | | | |-CopyConstructor simple trivial has_const_param implicit_has_const_param 20 | | | |-MoveConstructor 21 | | | |-CopyAssignment trivial has_const_param needs_implicit implicit_has_const_param 22 | | | |-MoveAssignment 23 | | | `-Destructor non_trivial user_declared 24 | | |-CXXRecordDecl 0x7ea14a8 <col:1, col:7> col:7 implicit referenced class myClass 25 | | |-AccessSpecDecl 0x7ea1538 <line:3:3, col:9> col:3 public 26 | | |-CXXConstructorDecl 0x7ea15e8 <line:4:5, col:23> col:5 used myClass 'void () noexcept' default trivial 27 | | | `-CompoundStmt 0x7ed1488 <col:23> 28 | | |-CXXDestructorDecl 0x7ea16d8 <line:5:5, col:17> col:5 used ~myClass 'void () noexcept' 29 | | | `-CompoundStmt 0x7ea1b88 <col:16, col:17> 30 | | |-CXXMethodDecl 0x7ea1858 <line:7:5, col:27> col:10 used myMehtod1 'void ()' 31 | | | `-CompoundStmt 0x7ea1b98 <col:26, col:27> 32 | | |-CXXMethodDecl 0x7ea1990 <line:8:5, col:27> col:10 used myMehtod2 'void ()' 33 | | | `-CompoundStmt 0x7ea1ba8 <col:26, col:27> 34 | | |-AccessSpecDecl 0x7ea1a30 <line:9:3, col:10> col:3 private 35 | | |-FieldDecl 0x7ea1a70 <line:10:5, col:9> col:9 a 'int' 36 | | |-FieldDecl 0x7ea1ad8 <line:11:5, col:11> col:11 b 'float' 37 | | `-CXXConstructorDecl 0x7ed1308 <line:2:7> col:7 implicit constexpr myClass 'void (const myClass &)' inline default trivial noexcept-unevaluated 0x7ed1308 38 | | `-ParmVarDecl 0x7ed1418 <col:7> col:7 'const myClass &' 39 | |-CXXRecordDecl 0x7ea1bb8 <line:14:1, line:17:1> line:14:8 struct myStruct definition 40 | | |-DefinitionData empty standard_layout has_user_declared_ctor can_const_default_init 41 | | | |-DefaultConstructor exists non_trivial user_provided defaulted_is_constexpr 42 | | | |-CopyConstructor simple trivial has_const_param needs_implicit implicit_has_const_param 43 | | | |-MoveConstructor 44 | | | |-CopyAssignment trivial has_const_param needs_implicit implicit_has_const_param 45 | | | |-MoveAssignment 46 | | | `-Destructor non_trivial user_declared 47 | | |-CXXRecordDecl 0x7ea1cd8 <col:1, col:8> col:8 implicit referenced struct myStruct 48 | | |-CXXConstructorDecl 0x7ea1dd0 <line:15:3, col:12> col:3 myStruct 'void ()' 49 | | `-CXXDestructorDecl 0x7ea1ec0 <line:16:3, col:13> col:3 ~myStruct 'void ()' noexcept-unevaluated 0x7ea1ec0 50 | |-CXXRecordDecl 0x7ea1fa0 <line:19:1, line:22:1> line:19:7 union myUnion definition 51 | | |-DefinitionData pass_in_registers aggregate standard_layout trivially_copyable pod trivial literal has_variant_members 52 | | | |-DefaultConstructor exists trivial needs_implicit 53 | | | |-CopyConstructor simple trivial has_const_param needs_implicit implicit_has_const_param 54 | | | |-MoveConstructor exists simple trivial needs_implicit 55 | | | |-CopyAssignment trivial has_const_param needs_implicit implicit_has_const_param 56 | | | |-MoveAssignment exists simple trivial needs_implicit 57 | | | `-Destructor simple irrelevant trivial needs_implicit 58 | | |-CXXRecordDecl 0x7ea20b8 <col:1, col:7> col:7 implicit union myUnion 59 | | |-FieldDecl 0x7ea2160 <line:20:3, col:7> col:7 a 'int' 60 | | `-FieldDecl 0x7ea21c8 <line:21:3, col:10> col:10 b 'double' 61 | |-FunctionDecl 0x7ed0dc8 <line:24:1, col:21> col:6 myFunc1 'void ()' 62 | | `-CompoundStmt 0x7ed0eb0 <col:20, col:21> 63 | |-FunctionDecl 0x7ed0f58 <line:25:1, col:21> col:6 myFunc2 'void ()' 64 | | `-CompoundStmt 0x7ed0ff8 <col:20, col:21> 65 | `-FunctionDecl 0x7ed11b0 <line:27:1, line:36:1> line:27:5 main 'int (int, char **)' 66 |  |-ParmVarDecl 0x7ed1020 <col:11, col:15> col:15 argc 'int' 67 |  |-ParmVarDecl 0x7ed10d0 <col:21, col:28> col:28 argv 'char **' 68 |  `-CompoundStmt 0x7ed1918 <col:34, line:36:1> 69 |  |-DeclStmt 0x7ed14c0 <line:28:3, col:13> 70 |  | `-VarDecl 0x7ed1270 <col:3, col:11> col:11 used mc 'myClass' callinit destroyed 71 |  | `-CXXConstructExpr 0x7ed1498 <col:11> 'myClass' 'void () noexcept' 72 |  |-CXXMemberCallExpr 0x7ed1528 <line:29:3, col:16> 'void' 73 |  | `-MemberExpr 0x7ed14f8 <col:3, col:6> '' .myMehtod1 0x7ea1858 74 |  | `-DeclRefExpr 0x7ed14d8 <col:3> 'myClass' lvalue Var 0x7ed1270 'mc' 'myClass' 75 |  |-CXXMemberCallExpr 0x7ed1598 <line:30:3, col:16> 'void' 76 |  | `-MemberExpr 0x7ed1568 <col:3, col:6> '' .myMehtod2 0x7ea1990 77 |  | `-DeclRefExpr 0x7ed1548 <col:3> 'myClass' lvalue Var 0x7ed1270 'mc' 'myClass' 78 |  |-DeclStmt 0x7ed1638 <line:31:3, col:8> 79 |  | `-VarDecl 0x7ed15d0 <col:3, col:7> col:7 used a 'int' 80 |  |-DeclStmt 0x7ed16d0 <line:32:3, col:10> 81 |  | `-VarDecl 0x7ed1668 <col:3, col:9> col:9 b 'float' 82 |  |-DeclStmt 0x7ed1800 <line:33:3, col:10> 83 |  | |-VarDecl 0x7ed1700 <col:3, col:7> col:7 used c 'int' 84 |  | `-VarDecl 0x7ed1780 <col:3, col:9> col:9 used d 'int' 85 |  |-BinaryOperator 0x7ed18c8 <line:34:3, col:9> 'int' lvalue '=' 86 |  | |-DeclRefExpr 0x7ed1818 <col:3> 'int' lvalue Var 0x7ed15d0 'a' 'int' 87 |  | `-BinaryOperator 0x7ed18a8 <col:7, col:9> 'int' '+' 88 |  | |-ImplicitCastExpr 0x7ed1878 <col:7> 'int' <LValueToRValue> 89 |  | | `-DeclRefExpr 0x7ed1838 <col:7> 'int' lvalue Var 0x7ed1700 'c' 'int' 90 |  | `-ImplicitCastExpr 0x7ed1890 <col:9> 'int' <LValueToRValue> 91 |  | `-DeclRefExpr 0x7ed1858 <col:9> 'int' lvalue Var 0x7ed1780 'd' 'int' 92 |  `-ReturnStmt 0x7ed1908 <line:35:3, col:10> 93 |  `-IntegerLiteral 0x7ed18e8 <col:10> 'int' 0 94 | -------------------------------------------------------------------------------- /test/main.cpp: -------------------------------------------------------------------------------- 1 | 2 | class myClassmain { 3 | public: 4 | myClassmain() = default; 5 | ~myClassmain() {} 6 | 7 | void myMehtod1(void) {} 8 | void myMehtod2(void) {} 9 | 10 | private: 11 | int a; 12 | float b; 13 | }; 14 | 15 | struct myStruct { 16 | myStruct(); 17 | ~myStruct(); 18 | }; 19 | 20 | union myUnion { 21 | int a; 22 | double b; 23 | int app; 24 | }; 25 | 26 | struct verymuchStruct { 27 | int myinteger; 28 | int yourinteger; 29 | int ourinteger; 30 | }; 31 | 32 | void myFunc1(void) {} 33 | void myFunc2(void) {} 34 | 35 | int main(int argc, char **argv) { 36 | myClassmain mc; 37 | mc.myMehtod1(); 38 | mc.myMehtod2(); 39 | int a; 40 | float b; 41 | int c, d; 42 | a = c + d; 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /test/makefile: -------------------------------------------------------------------------------- 1 | TARGET?=main 2 | SHELL=bash 3 | SHELL?=bash 4 | CXX=clang++ 5 | CXX?=clang++ 6 | CXX_FLAGS=-std=c++11 -fpic 7 | CXX_EXTRA?= 8 | CTAGS_I_PATH?=./ 9 | LD_FLAGS= 10 | EXTRA_LD_FLAGS?= 11 | ADD_SANITIZERS_CC= -g -fsanitize=address -fno-omit-frame-pointer 12 | ADD_SANITIZERS_LD= -g -fsanitize=address 13 | MEM_SANITIZERS_CC= -g -fsanitize=memory -fno-omit-frame-pointer 14 | MEM_SANITIZERS_LD= -g -fsanitize=memory 15 | UB_SANITIZERS_CC= -g -fsanitize=undefined -fno-omit-frame-pointer 16 | UB_SANITIZERS_LD= -g -fsanitize=undefined 17 | COV_CXX= -fprofile-instr-generate -fcoverage-mapping 18 | COV_LD= -fprofile-instr-generate 19 | # BUILD_MODES are=RELEASE(default), DEBUG,ADDSAN,MEMSAN,UBSAN 20 | BUILD_MODE?=RELEASE 21 | OBJ_LIST:=$(patsubst %.cpp, %.o, $(wildcard *.cpp)) 22 | ASM_LIST:=$(patsubst %.cpp, %.dis, $(wildcard *.cpp)) 23 | WASM_LIST:=$(patsubst %.cpp, %.wasm, $(wildcard *.cpp)) 24 | JS_LIST:=$(patsubst %.cpp, %.js, $(wildcard *.cpp)) 25 | 26 | ifeq ($(BUILD_MODE), ADDSAN) 27 | ifeq ($(CXX), g++) 28 | $(error This build mode is only useable with clang++.) 29 | endif 30 | CXX_EXTRA+=$(ADD_SANITIZERS_CC) 31 | EXTRA_LD_FLAGS+=$(ADD_SANITIZERS_LD) 32 | endif 33 | 34 | ifeq ($(BUILD_MODE), MEMSAN) 35 | ifeq ($(CXX), g++) 36 | $(error This build mode is only useable with clang++.) 37 | endif 38 | CXX_EXTRA+=$(MEM_SANITIZERS_CC) 39 | EXTRA_LD_FLAGS+=$(MEM_SANITIZERS_LD) 40 | endif 41 | 42 | ifeq ($(BUILD_MODE), UBSAN) 43 | ifeq ($(CXX), g++) 44 | $(error This build mode is only useable with clang++.) 45 | endif 46 | CXX_EXTRA+=$(UB_SANITIZERS_CC) 47 | EXTRA_LD_FLAGS+=$(UB_SANITIZERS_LD) 48 | endif 49 | 50 | SRCS:=$(wildcard *.cpp) 51 | HDRS:=$(wildcard *.h) 52 | CXX_FLAGS+=$(CXX_EXTRA) 53 | LD_FLAGS+=$(EXTRA_LD_FLAGS) 54 | 55 | .DEFAULT:all 56 | 57 | .PHONY:all clean help ASM SO TAGS WASM JS 58 | 59 | all:$(TARGET) 60 | 61 | everything:$(TARGET) A ASM SO $(TARGET)-static $(TARGET)-dbg TAGS $(TARGET)-cov WASM JS 62 | 63 | depend:.depend 64 | 65 | .depend:$(SRCS) 66 | rm -rf .depend 67 | $(CXX) -MM $(CXX_FLAGS) $^ > ./.depend 68 | echo $(patsubst %.o:, %.odbg:, $(shell $(CXX) -MM $(CXX_FLAGS) $^)) | sed -r 's/[A-Za-z0-9\-\_]+\.odbg/\n&/g' >> ./.depend 69 | echo $(patsubst %.o:, %.ocov:, $(shell $(CXX) -MM $(CXX_FLAGS) $^)) | sed -r 's/[A-Za-z0-9\-\_]+\.ocov/\n&/g' >> ./.depend 70 | 71 | -include ./.depend 72 | 73 | .cpp.o: 74 | $(CXX) $(CXX_FLAGS) -c $< -o $@ 75 | 76 | %.odbg:%.cpp 77 | $(CXX) $(CXX_FLAGS) -g -c $< -o $@ 78 | 79 | %.ocov:%.cpp 80 | $(CXX) $(CXX_FLAGS) $(COV_CXX) -c $< -o $@ 81 | 82 | $(TARGET): $(OBJ_LIST) 83 | $(CXX) $(LD_FLAGS) $^ -o $@ 84 | 85 | $(TARGET)-static: $(TARGET).o 86 | $(CXX) $(LD_FLAGS) $^ -static -o $@ 87 | 88 | $(TARGET)-dbg: $(TARGET).odbg 89 | $(CXX) $(LD_FLAGS) $^ -g -o $@ 90 | 91 | $(TARGET)-cov: $(TARGET).ocov 92 | $(CXX) $(LD_FLAGS) $^ $(COV_LD) -o $@ 93 | 94 | cov: runcov 95 | @llvm-profdata merge -sparse ./default.profraw -o ./default.profdata 96 | @llvm-cov show $(TARGET)-cov -instr-profile=default.profdata 97 | 98 | covrep: runcov 99 | @llvm-profdata merge -sparse ./default.profraw -o ./default.profdata 100 | @llvm-cov report $(TARGET)-cov -instr-profile=default.profdata 101 | 102 | ASM:$(ASM_LIST) 103 | 104 | SO:$(TARGET).so 105 | 106 | A:$(TARGET).a 107 | 108 | WASM:$(WASM_LIST) 109 | 110 | JS:$(JS_LIST) 111 | 112 | TAGS:tags 113 | 114 | #https://github.com/rizsotto/Bear 115 | BEAR: clean 116 | bear make 117 | 118 | tags:$(SRCS) 119 | $(shell $(CXX) -c -I $(CTAGS_I_PATH) -M $(SRCS)|\ 120 | sed -e 's/[\\ ]/\n/g'|sed -e '/^$$/d' -e '/\.o:[ \t]*$$/d'|\ 121 | ctags -L - --c++-kinds=+p --fields=+iaS --extra=+q) 122 | 123 | %.dis: %.o 124 | objdump -r -d -M intel -S $< > $@ 125 | 126 | %.wasm: %.cpp 127 | $(CXX) --compile $< --target=wasm32-unknown-unknown-wasm --output $@ 128 | 129 | %.js: %.cpp 130 | em++ $< -o $@ 131 | 132 | $(TARGET).so: $(TARGET).o 133 | $(CXX) $(LD_FLAGS) $^ -shared -o $@ 134 | 135 | $(TARGET).a: $(TARGET).o 136 | ar rcs $(TARGET).a $(TARGET).o 137 | 138 | runcov: $(TARGET)-cov 139 | $(TARGET)-cov 140 | 141 | test: $(TARGET) 142 | $(TARGET) 143 | 144 | valgrind: $(TARGET) 145 | - valgrind --track-origins=yes --leak-check=full --show-leak-kinds=all $(TARGET) 146 | 147 | format: 148 | - clang-format -i $(SRCS) $(HDRS) 149 | 150 | clean: 151 | rm -f *.o *.dis *.odbg *.ocov *.js *~ $(TARGET) $(TARGET).so $(TARGET)-static $(TARGET)-dbg $(TARGET).a $(TARGET)-cov 152 | 153 | deepclean: clean 154 | - rm tags 155 | - rm .depend 156 | - rm ./default.profraw ./default.profdata 157 | - rm vgcore.* 158 | - rm compile_commands.json 159 | 160 | help: 161 | @echo "--all is the default target, runs $(TARGET) target" 162 | @echo "--everything will build everything" 163 | @echo "--SO will generate the so" 164 | @echo "--ASM will generate assembly files" 165 | @echo "--TAGS will generate tags file" 166 | @echo "--$(TARGET) builds the dynamically-linked executable" 167 | @echo "--$(TARGET)-dbg will generate the debug build. BUILD_MODE should be set to DEBUG to work" 168 | @echo "--$(TARGET)-static will statically link the executable to the libraries" 169 | @echo "--$(TARGET)-cov is the coverage build" 170 | @echo "--cov will print the coverage report" 171 | @echo "--covrep will print the line coverage report" 172 | @echo "--A will build the static library" 173 | @echo "--TAGS will build the tags file" 174 | @echo "--clean" 175 | @echo "--deepclean will clean almost everything" 176 | -------------------------------------------------------------------------------- /test/nameddecldef.cpp: -------------------------------------------------------------------------------- 1 | 2 | namespace nameddecldef_ns{ 3 | int testVar; 4 | struct testStruct { 5 | int a; 6 | int b; 7 | char *c; 8 | }; 9 | 10 | void testFunction(void); 11 | void testFunction(void) { return; } 12 | 13 | class testClass { 14 | public: 15 | testClass(); 16 | virtual ~testClass(); 17 | 18 | private: 19 | int myPrivate; 20 | }; 21 | }; // namespace 22 | -------------------------------------------------------------------------------- /test/structdecl.cpp: -------------------------------------------------------------------------------- 1 | 2 | namespace structdecl_ns{ 3 | struct testStruct { 4 | int a; 5 | float b; 6 | char c[10]; 7 | }; 8 | 9 | #define structdeclmacro structDeclMacroExpanded 10 | struct structdeclmacro { 11 | int d; 12 | double e; 13 | }; 14 | } // namespace 15 | -------------------------------------------------------------------------------- /test/test_list.md: -------------------------------------------------------------------------------- 1 | 2 | ## Test List 3 | 4 | - [x] function declaration 5 | - [x] field declratation 6 | - [x] cxx method declaration 7 | - [x] variable declaration 8 | - [x] class declaration 9 | - [x] struct declaration 10 | - [x] union declaration/definition 11 | - [x] named declaration/definition 12 | - [x] declaration reference expression 13 | - [x] call expression 14 | - [x] cxx member call expression 15 | - [x] cxx record declaration 16 | - [] macro definition 17 | - [] header inclusion directive 18 | 19 | ## Cross Test List 20 | 21 | - macro expansion behaviour 22 | - overload 23 | -------------------------------------------------------------------------------- /test/uniondecdef.cpp: -------------------------------------------------------------------------------- 1 | 2 | namespace uniondecl_ns{ 3 | union testUnion { 4 | int reg; 5 | bool b1; 6 | bool b2; 7 | }; 8 | 9 | #define uniondecdefmacro unionDecDefMacroExpanded 10 | union uniondecdefmacro { 11 | int reggie; 12 | int bubu; 13 | }; 14 | }; // namespace 15 | -------------------------------------------------------------------------------- /test/vardecl.cpp: -------------------------------------------------------------------------------- 1 | 2 | namespace vardecl_ns{ 3 | int gtesta; 4 | 5 | int testFunction(int a, int b); 6 | int testFunction(int a, int b) { return a + b; } 7 | 8 | class testClass { 9 | public: 10 | testClass(int a) : testa(a) {} 11 | virtual ~testClass(); 12 | 13 | void cxxMethod(double a) {} 14 | 15 | private: 16 | int testa; 17 | }; 18 | 19 | #define vardeclmacro varDeclMacroExpanded 20 | int testvardeclmacro; 21 | }; // namespace 22 | -------------------------------------------------------------------------------- /testscript/main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # _*_ coding=utf-8 _*_ 3 | 4 | import argparse 5 | import subprocess 6 | import os 7 | import sys 8 | import re 9 | 10 | test_files = ["/home/bloodstalker/extra/cgrep/cgrep.cpp"] 11 | 12 | test_files_2 = [ 13 | "./test/function.cpp", 14 | "./test/fielddecl.cpp", 15 | "./test/cxxmethoddecl.cpp", 16 | "./test/vardecl.cpp", 17 | "./test/classdecl.cpp", 18 | "./test/structdecl.cpp", 19 | "./test/uniondecdef.cpp", 20 | "./test/nameddecldef.cpp", 21 | "./test/declrefexpr.cpp", 22 | "./test/callexpr.cpp", 23 | "./test/cxxmembercallexpr.cpp", 24 | ] 25 | 26 | cgrep_test_args = [ 27 | "-A 1 -B 1 --func --var --regex n[aA]m", 28 | "-A 1 -B 1 --func --var --awk --regex n[aA]m", 29 | "-A 1 -B 1 --func --declrefexpr --regex n[aA]m --nocolor", 30 | "-A 1 -B 1 --func --declrefexpr --memfunc --call --cxxcall --var --regex run", 31 | "-A 1 -B 1 --macro --header --regex n[aA]m", 32 | "-A 1 -B 1 --class --regex and", 33 | "-A 1 -B 1 --struct --union --regex n[aA]m", 34 | "-A 1 -B 1 --nameddecl --regex n[aA]m", 35 | "-A 1 -B 1 --cxxcall --call --regex add", 36 | "-A 1 -B 1 --cfield --regex ite", 37 | "--union --regex [Uu]nion ./test/main.cpp", 38 | "--struct --regex [sS]truct ./test/main.cpp", 39 | "--dir ./ --regex run --func", ] 40 | 41 | cgrep_test_args_2 = [ 42 | "--func --regex test", 43 | "--cxxfield --regex test", 44 | "--memfunc --regex test", 45 | "--var --regex test", 46 | "--class --regex test", 47 | "--struct --regex test", 48 | "--union --regex test", 49 | "--nameddecl --regex test", 50 | "--declrefexpr --regex test", 51 | "--call --regex test", 52 | "--cxxcall --regex test", 53 | ] 54 | 55 | 56 | class Argparser(object): 57 | def __init__(self): 58 | parser = argparse.ArgumentParser() 59 | parser.add_argument("--string", type=str, help="string") 60 | parser.add_argument("--capturereference", type=str, 61 | help="capture a new reference for the test outputs using the current outputs") 62 | parser.add_argument("--compare", action="store_true", 63 | help="compare the current test results with the reference", default=False) 64 | self.args = parser.parse_args() 65 | 66 | 67 | def call_from_shell(command, *command_args): 68 | command_list = [arg for arg in command_args] 69 | command_list.insert(0, command) 70 | if sys.version_info < (3, 7): 71 | return subprocess.run(command_list, stdout=subprocess.PIPE) 72 | else: 73 | return subprocess.run(command_list, capture_output=True) 74 | 75 | 76 | def call_from_shell_list(command_list): 77 | if sys.version_info < (3, 7): 78 | return subprocess.run(command_list, stdout=subprocess.PIPE) 79 | else: 80 | return subprocess.run(command_list, capture_output=True) 81 | 82 | 83 | def call_from_shell_pprint(command, *command_args): 84 | print(call_from_shell(command, *command_args).stdout.decode("utf-8")) 85 | 86 | 87 | def call_from_shell_list_pprint(command_list): 88 | print(call_from_shell_list(command_list).stdout.decode("utf-8")) 89 | 90 | 91 | def main(): 92 | # argparser = Argparser() 93 | cgrep_exe = "cgrep" 94 | os.chdir("../") 95 | # get LLVM libdir 96 | llvm_libdir = call_from_shell_list(["llvm-config", "--libdir"]) 97 | # get LLVM version. upstream builds can have extra unwanted text attached. 98 | llvm_version = re.findall("[0-9]*\.[0-9]*\.[0-9]*", call_from_shell_list( 99 | ["llvm-config", "--version"]).stdout.decode("utf-8")) 100 | # buld the magic sause. we dont wanna get stddef.h not found. 101 | clang_builtin_headers = "--extra-arg=-I" + \ 102 | llvm_libdir.stdout.decode( 103 | "utf-8")[:-1] + "/clang/" + llvm_version[0] + "/include" 104 | for cgrep_test_arg, cgrep_test_files in zip(cgrep_test_args_2, test_files_2): 105 | arg_list = cgrep_test_arg.split() 106 | arg_list.insert(0, cgrep_exe) 107 | arg_list.insert(1, clang_builtin_headers) 108 | arg_list.append(cgrep_test_files) 109 | print(arg_list) 110 | ret = call_from_shell_list(arg_list) 111 | print("ret:", ret.stdout.decode("utf-8"), end="") 112 | print("ret:", ret.stderr.decode("utf-8"), end="") 113 | 114 | 115 | if __name__ == "__main__": 116 | main() 117 | --------------------------------------------------------------------------------