fccf
検索文字列に基づいてディレクトリ内の C/C++ ソース コードをすばやく検索し、クエリに一致する関連コード スニペットを出力するコマンド ライン ツールです。
次のビデオは、 fccf
torvalds/linux でコードのスニペットを検索して見つける様子を示しています。
fccf
ソース ツリーの検索 (最新の C++)次のビデオは、 fccf
fccf
C++ ソース コードを検索する様子を示しています。
ここでの検索結果には次のものが含まれることに注意してください。
--flag
のいずれかに一致するものを検索します任意の--flag
、たとえば任意の enum 宣言に一致する空のクエリを提供します。
...または任意のクラス コンストラクター
for
ステートメントを検索するステートメントfor
検索するには、 --for-statement
を使用します。 fccf
クエリ文字列を含むfor
ステートメント (C++ ranged for
を含む) を検索しようとします。
--include-expressions
オプションを使用して、クエリに一致する式を検索します。
次の例は、 fccf
find によるisdigit()
の呼び出しを示しています。
次の例は、 fccf
clang_options
変数への参照を検索することを示しています。
using
宣言の検索--using-declaration
オプションを使用して、 using
宣言、 using
ディレクティブ、および型エイリアス宣言を検索します。
namespace
エイリアスの検索--namespace-alias
オプションを使用して、 namespace
エイリアスを検索します。
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/
で終わるパスなど) を特定します。次に、これらのパスを (翻訳単位を解析する前に) Clang オプションに-Ifoo -Ibar/baz
などとして追加します。さらに、各翻訳単位について、親パスと祖父母パスもその単位のインクルード ディレクトリに追加されます。解析が成功する可能性が高くなります。
-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);
}