In unserem vorherigen Post Wie man C++-‘struct’-Definition aus Quellcode mit clang parst haben wir gelernt, wie man eine C++-struct-Definition mit dem clang-C++-Parser parst. In diesem Post zeigen wir, wie man C++-Kommentare aus Quellcode mit clang parst.
parse_comments.cpp
#include <clang-c/Index.h>
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
void extractCommentBlocks(CXTranslationUnit unit) {
CXSourceRange range = clang_getCursorExtent(clang_getTranslationUnitCursor(unit));
CXToken *tokens;
unsigned numTokens;
clang_tokenize(unit, range, &tokens, &numTokens);
for (unsigned i = 0; i < numTokens; ++i) {
CXTokenKind tokenKind = clang_getTokenKind(tokens[i]);
if (tokenKind == CXToken_Comment) {
CXString tokenSpelling = clang_getTokenSpelling(unit, tokens[i]);
CXSourceLocation location = clang_getTokenLocation(unit, tokens[i]);
unsigned line, column;
CXFile file;
clang_getFileLocation(location, &file, &line, &column, nullptr);
std::cout << "Comment at line " << line << ", column " << column << ": "
<< clang_getCString(tokenSpelling) << std::endl;
clang_disposeString(tokenSpelling);
}
}
clang_disposeTokens(unit, tokens, numTokens);
}
int main() {
CXIndex index = clang_createIndex(0, 0);
const char *code = R"(
// Dies ist ein einzeiliger Kommentar
typedef struct {
double a; /* That weird parameter */
double b; /* Another weird parameter */
} myParameters;
/*
* This is a multi-line
* comment block
*/
int function() {
// Another single line comment
return 42; /* inline comment */
}
)";
CXUnsavedFile unsavedFile = {"test.cpp", code, (unsigned long)strlen(code)};
CXTranslationUnit unit = clang_parseTranslationUnit(index, "test.cpp", nullptr, 0, &unsavedFile, 1, CXTranslationUnit_None);
if (unit == nullptr) {
std::cerr << "Failed to parse translation unit." << std::endl;
return 1;
}
CXCursor cursor = clang_getTranslationUnitCursor(unit);
extractCommentBlocks(unit);
clang_disposeTranslationUnit(unit);
clang_disposeIndex(index);
return 0;
}Wie man kompiliert
Installiere auf Ubuntu die erforderlichen Bibliotheken mit
install_libclang19.sh
sudo apt -y install libclang-19-devund kompiliere den Code mit
build_parse_comments.sh
g++ parse_comments.cpp -o parse_comments -std=c++17 -I/usr/lib/llvm-19/include -L/usr/lib/llvm-19/lib -lclangBeispiel-Ausgabe
Ausführen mit ./parse_comments
parse_comments_output.txt
Comment at line 2, column 9: // Dies ist ein einzeiliger Kommentar
Comment at line 4, column 23: /* That weird parameter */
Comment at line 5, column 23: /* Another weird parameter */
Comment at line 8, column 9: /*
* This is a multi-line
* comment block
*/
Comment at line 13, column 13: // Another single line comment
Comment at line 14, column 24: /* inline comment */Alternative Build mit cmake
Du musst möglicherweise die Versionen (19.1.1) und andere Parameter hier anpassen, um es für dein Setup ordnungsgemäß zum Laufen zu bringen.
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(parse_comments)
# C++17 Standard festlegen
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# LLVM/Clang-Komponenten finden
find_package(LLVM 19.1.1 REQUIRED CONFIG)
find_package(Clang REQUIRED CONFIG)
# Include-Verzeichnisse hinzufügen
include_directories(${LLVM_INCLUDE_DIRS})
include_directories(${CLANG_INCLUDE_DIRS})
# Executable erstellen
add_executable(parse_comments parse_comments.cpp)
# LLVM-Definitionen hinzufügen
target_compile_definitions(parse_comments PUBLIC ${LLVM_DEFINITIONS})
# Gegen libclang linken
target_link_libraries(parse_comments PUBLIC libclang)
Ähnliche Beiträge nach Kategorie:
C/C++ Clang Source Introspection
Wenn dieser Beitrag dir geholfen hat, erwäge, mir einen Kaffee zu spendieren oder per PayPal zu spenden, um die Recherche und Veröffentlichung neuer Beiträge auf TechOverflow zu unterstützen