fccf
是命令列工具,可根據搜尋字串快速搜尋目錄中的 C/C++ 原始程式碼,並列印與查詢相符的相關程式碼片段。
以下影片展示了fccf
在 torvalds/linux 中搜尋和尋找程式碼片段。
fccf
原始碼樹(現代 C++)以下影片顯示了fccf
搜尋fccf
C++ 原始碼。
請注意,此處的搜尋結果包括:
--flag
提供一個空查詢來匹配任何--flag
,例如任何枚舉聲明。
...或任何類別構造函數
for
語句使用--for-statement
for
語句。 fccf
將嘗試尋找包含查詢字串的for
語句(包括 C++ 範圍內的for
)。
使用--include-expressions
選項尋找與查詢相符的表達式。
以下範例顯示了fccf
find 對isdigit()
呼叫。
以下範例顯示fccf
尋找clang_options
變數的參考。
using
聲明使用--using-declaration
選項尋找using
宣告、 using
指令和型別別名宣告。
namespace
別名使用--namespace-alias
選項尋找namespace
別名。
throw
表達式將--throw-expression
與查詢一起使用可搜尋包含查詢字串的特定throw
表達式。
如前所述,這裡的空查詢將嘗試匹配程式碼庫中的任何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/
結尾的路徑。然後,它將這些路徑新增至 clang 選項(在解析翻譯單元之前),如-Ifoo -Ibar/baz
等。中,以便增加成功解析的可能性。
也可以使用-I
或--include-dir
選項向fccf
提供其他包含目錄。使用詳細輸出( --verbose
),可以識別 libclang 解析中的錯誤並嘗試修復(例如,添加正確的包含目錄,以便libclang
滿意)。
為了在fccf
原始碼上執行fccf
而沒有任何 libclang 錯誤,我必須明確提供 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);
}