fccf
는 검색 문자열을 기반으로 디렉터리의 C/C++ 소스 코드를 빠르게 검색하고 쿼리와 일치하는 관련 코드 조각을 인쇄하는 명령줄 도구입니다.
다음 비디오는 fccf
torvalds/linux에서 코드 조각을 검색하고 찾는 것을 보여줍니다.
fccf
소스 트리 검색(최신 C++) 다음 비디오는 fccf
C++ 소스 코드를 검색하는 fccf
보여줍니다.
여기 검색결과에는 다음이 포함됩니다.
--flag
중 하나를 검색합니다. --flag
(예: 열거형 선언)와 일치하는 빈 쿼리를 제공하세요.
... 또는 클래스 생성자
for
문 검색 명령문을 for
하려면 --for-statement
사용하십시오. fccf
쿼리 문자열이 포함된 for
문( for
범위의 C++ 포함)을 찾으려고 시도합니다.
쿼리와 일치하는 표현식을 찾으려면 --include-expressions
옵션을 사용하십시오.
다음 예에서는 isdigit()
에 대한 fccf
find 호출을 보여줍니다.
다음 예에서는 fccf
clang_options
변수에 대한 참조를 찾는 것을 보여줍니다.
using
선언 검색 using
선언, using
지시문 및 유형 별칭 선언을 찾으려면 --using-declaration
옵션을 사용합니다.
namespace
별칭 검색 namespace
별칭을 찾으려면 --namespace-alias
옵션을 사용합니다.
throw
표현 검색하기 쿼리 문자열이 포함된 특정 throw
표현식을 검색하려면 쿼리와 함께 --throw-expression
사용하세요.
앞서 제시된 것처럼 여기의 빈 쿼리는 코드 베이스의 모든 throw
표현식과 일치하려고 시도합니다.
CMake를 사용하여 fccf
빌드합니다. 자세한 내용은 BUILDING.md를 참조하세요.
참고: fccf
사용하려면 libclang
및 LLVM
설치되어 있어야 합니다.
# Install libclang and LLVM
# sudo apt install libclang-dev llvm
git clone https://github.com/p-ranav/fccf
cd fccf
# Build
cmake -S . -B build -D CMAKE_BUILD_TYPE=Release
cmake --build build
# Install
sudo cmake --install build
fccf
사용법 foo@bar:~ $ fccf --help
Usage: fccf [--help] [--version] [--help] [--exact-match] [--json] [--filter VAR] [-j VAR] [--enum] [--struct] [--union] [--member-function] [--function] [--function-template] [-F] [--class] [--class-template] [--class-constructor] [--class-destructor] [-C] [--for-statement] [--namespace-alias] [--parameter-declaration] [--typedef] [--using-declaration] [--variable-declaration] [--verbose] [--include-expressions] [--static-cast] [--dynamic-cast] [--reinterpret-cast] [--const-cast] [-c] [--throw-expression] [--ignore-single-line-results] [--include-dir VAR]... [--language VAR] [--std VAR] [--no-color] query [path]...
Positional arguments:
query
path [nargs: 0 or more]
Optional arguments:
-h, --help shows help message and exits
-v, --version prints version information and exits
-h, --help Shows help message and exits
-E, --exact-match Only consider exact matches
--json Print results in JSON format
-f, --filter Only evaluate files that match filter pattern [nargs=0..1] [default: "*.*"]
-j Number of threads [nargs=0..1] [default: 5]
--enum Search for enum declaration
--struct Search for struct declaration
--union Search for union declaration
--member-function Search for class member function declaration
--function Search for function declaration
--function-template Search for function template declaration
-F Search for any function or function template or class member function
--class Search for class declaration
--class-template Search for class template declaration
--class-constructor Search for class constructor declaration
--class-destructor Search for class destructor declaration
-C Search for any class or class template or struct
--for-statement Search for `for` statement
--namespace-alias Search for namespace alias
--parameter-declaration Search for function or method parameter
--typedef Search for typedef declaration
--using-declaration Search for using declarations, using directives, and type alias declarations
--variable-declaration Search for variable declaration
--verbose Request verbose output
--ie, --include-expressions Search for expressions that refer to some value or member, e.g., function, variable, or enumerator.
--static-cast Search for static_cast
--dynamic-cast Search for dynamic_cast
--reinterpret-cast Search for reinterpret_cast
--const-cast Search for const_cast
-c Search for any static_cast, dynamic_cast, reinterpret_cast, orconst_cast expression
--throw-expression Search for throw expression
--isl, --ignore-single-line-results Ignore forward declarations, member function declarations, etc.
-I, --include-dir Additional include directories [nargs=0..1] [default: {}] [may be repeated]
-l, --language Language option used by clang [nargs=0..1] [default: "c++"]
--std C++ standard to be used by clang [nargs=0..1] [default: "c++17"]
--nc, --no-color Stops fccf from coloring the output
fccf
grep
또는 ripgrep
과 같이 건초 더미에서 바늘에 대한 재귀 디렉터리 검색을 수행합니다. 가능하면 SSE2
strstr
SIMD 알고리즘(수정된 Rabin-Karp SIMD 검색, 여기 참조)을 사용하여 여러 스레드에서 바늘이 포함된 디렉터리의 소스 파일입니다.libclang
사용하여 번역 단위를 구문 분석합니다(추상 구문 트리 구축).CXCursor_FunctionDecl
을 찾습니다.libclang
이름)이 검색 쿼리와 일치하면 AST 노드의 소스 범위가 식별됩니다. 소스 범위는 코드 조각의 시작 및 끝 인덱스입니다. 버퍼include_directories
에 대한 참고 사항 이 모든 것이 작동하려면 fccf는 먼저 헤더 파일(예: include/
로 끝나는 경로)을 포함하는 후보 디렉토리를 식별합니다. 그런 다음 번역 단위를 구문 분석하기 전에 -Ifoo -Ibar/baz
등으로 이러한 경로를 clang 옵션에 추가합니다. 또한 각 번역 단위에 대해 상위 및 상위 경로도 해당 단위의 포함 디렉터리에 추가됩니다. 성공적인 구문 분석 가능성을 높입니다.
-I
또는 --include-dir
옵션을 사용하여 추가 포함 디렉토리를 fccf
에 제공할 수도 있습니다. 자세한 출력( --verbose
)을 사용하면 libclang 구문 분석의 오류를 식별하고 수정을 시도할 수 있습니다(예: libclang
만족하도록 올바른 포함 디렉터리 추가).
libclang 오류 없이 fccf
소스 코드에서 fccf
실행하려면 다음과 같이 LLVM-12의 포함 경로를 명시적으로 제공해야 했습니다.
foo@bar:~ $ fccf --verbose ' lexer ' . --include-dir /usr/lib/llvm-12/include/
Checking ./source/lexer.cpp
Checking ./source/lexer.hpp
Checking ./source/searcher.cpp
// ./source/lexer.hpp (Line: 14 to 40)
class lexer
{
std::string_view m_input;
fmt::memory_buffer* m_out;
std::size_t m_index {0};
bool m_is_stdout {true};
char previous() const;
char current() const;
char next() const;
void move_forward(std::size_t n = 1);
bool is_line_comment();
bool is_block_comment();
bool is_start_of_identifier();
bool is_start_of_string();
bool is_start_of_number();
void process_line_comment();
void process_block_comment();
bool process_identifier(bool maybe_class_or_struct = false);
void process_string();
std::size_t get_number_of_characters(std::string_view str);
public:
void tokenize_and_pretty_print(std::string_view source,
fmt::memory_buffer* out,
bool is_stdout = true);
}