From patchwork Fri Jun 4 11:14:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Palethorpe X-Patchwork-Id: 1487742 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=suse.com header.i=@suse.com header.a=rsa-sha256 header.s=susede1 header.b=iCZ/V+/F; dkim-atps=neutral Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4FxKth362rz9sRf for ; Fri, 4 Jun 2021 21:15:24 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id CCC073C7FDB for ; Fri, 4 Jun 2021 13:15:21 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-7.smtp.seeweb.it (in-7.smtp.seeweb.it [IPv6:2001:4b78:1:20::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by picard.linux.it (Postfix) with ESMTPS id 2A2053C7FCE for ; Fri, 4 Jun 2021 13:14:47 +0200 (CEST) Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by in-7.smtp.seeweb.it (Postfix) with ESMTPS id 45412201082 for ; Fri, 4 Jun 2021 13:14:43 +0200 (CEST) Received: from relay2.suse.de (unknown [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 95F2621A1E; Fri, 4 Jun 2021 11:14:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1622805282; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jGgSQmPoPVbn7ihnkKEUJWcUiuI8boIBPzvc/ZSuCuk=; b=iCZ/V+/FC7VZixS/1llEy3MAUCHXIyG4lNogrrWqLRYaYtk1LFeknUKXsw69fV25iMK5p0 Q1xR/H+ZMBwhrCN6oxwCwuNWApZE976wqA5WoTDcC+AyFK5s+H/vREQRErw+WyT3Eyf8AG tnbeqB/5jdA7FF2E4alMZVNorr7Gg+I= Received: from g78.suse.de (unknown [10.163.24.38]) by relay2.suse.de (Postfix) with ESMTP id 65779A3B89; Fri, 4 Jun 2021 11:14:42 +0000 (UTC) To: ltp@lists.linux.it Date: Fri, 4 Jun 2021 12:14:33 +0100 Message-Id: <20210604111434.21422-2-rpalethorpe@suse.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210604111434.21422-1-rpalethorpe@suse.com> References: <20210604111434.21422-1-rpalethorpe@suse.com> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.102.4 at in-7.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.1 required=7.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on in-7.smtp.seeweb.it Subject: [LTP] [RFC PATCH v2 1/2] Add 'make check' and clang-check to build system X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Richard Palethorpe via ltp From: Richard Palethorpe Reply-To: Richard Palethorpe Cc: Richard Palethorpe Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Allows the user to run 'make check' to check all source files or 'make check-' to check one source file corresponding to a target. Adds makefile pieces for tools/clang-check/main which will be a libclang based tool. By default this is ran by 'make check'. In theory allows other tools to be specified with 'make CHECK=tool CHECK_FLAGS= check...'. e.g. 'make CHECK=sparse CHECK_FLAGS= check-tst_cgroup' Signed-off-by: Richard Palethorpe --- configure.ac | 2 ++ include/mk/clang-check.mk | 9 +++++++++ include/mk/config.mk.in | 5 +++++ include/mk/env_post.mk | 8 ++++++++ include/mk/generic_leaf_target.inc | 5 ++++- include/mk/lib.mk | 3 +++ include/mk/rules.mk | 9 +++++++++ include/mk/testcases.mk | 1 + tools/clang-check/.gitignore | 1 + tools/clang-check/Makefile | 14 ++++++++++++++ 10 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 include/mk/clang-check.mk create mode 100644 tools/clang-check/.gitignore create mode 100644 tools/clang-check/Makefile diff --git a/configure.ac b/configure.ac index 136d82d09..b37c13c3c 100644 --- a/configure.ac +++ b/configure.ac @@ -14,6 +14,7 @@ AC_CONFIG_FILES([ \ ]) AC_ARG_VAR(HOSTCC, [The C compiler on the host]) +AC_ARG_VAR(CLANG, [The LLVM Clang C compiler on the host]) AM_MAINTAINER_MODE([enable]) @@ -42,6 +43,7 @@ AC_CHECK_DECLS([SEM_STAT_ANY],,,[#include ]) AC_CHECK_HEADERS_ONCE([ \ asm/ldt.h \ + clang-c/Index.h \ ifaddrs.h \ keyutils.h \ linux/can.h \ diff --git a/include/mk/clang-check.mk b/include/mk/clang-check.mk new file mode 100644 index 000000000..2ab7b67a1 --- /dev/null +++ b/include/mk/clang-check.mk @@ -0,0 +1,9 @@ +# Rules to make clang-check tool(s) for inclusion in lib and testcases Makefiles + +CLANG_CHECK_DIR:= $(abs_top_builddir)/tools/clang-check + +$(CLANG_CHECK_DIR)/main: $(CLANG_CHECK_DIR) + $(MAKE) -C "$^" -f "$(CLANG_CHECK_DIR)/Makefile" all + +$(CLANG_CHECK_DIR): %: + mkdir -p "$@" diff --git a/include/mk/config.mk.in b/include/mk/config.mk.in index 218447ef3..361b6a746 100644 --- a/include/mk/config.mk.in +++ b/include/mk/config.mk.in @@ -44,6 +44,11 @@ HOSTCC := cc endif endif +CLANG := @CLANG@ +ifeq ($(strip $(CLANG)),) +CLANG := clang +endif + AIO_LIBS := @AIO_LIBS@ CAP_LIBS := @CAP_LIBS@ ACL_LIBS := @ACL_LIBS@ diff --git a/include/mk/env_post.mk b/include/mk/env_post.mk index 1d22f9c53..8903a934d 100644 --- a/include/mk/env_post.mk +++ b/include/mk/env_post.mk @@ -89,6 +89,14 @@ $(error You must define $$(prefix) before executing install) endif # END $(filter-out install,$(MAKECMDGOALS)),$(MAKECMDGOALS) endif +CHECK_TARGETS ?= $(addprefix check-,$(MAKE_TARGETS)) +CHECK ?= $(abs_top_srcdir)/tools/clang-check/main +CHECK_FLAGS ?= -resource-dir $(shell $(CLANG) -print-resource-dir) + +ifeq ($(dir $(CHECK)),$(abs_top_srcdir)/tools/clang-check/) +CHECK_DEPS += $(CHECK) +endif + include $(top_srcdir)/include/mk/rules.mk endif diff --git a/include/mk/generic_leaf_target.inc b/include/mk/generic_leaf_target.inc index 64953f89a..aa092a5a3 100644 --- a/include/mk/generic_leaf_target.inc +++ b/include/mk/generic_leaf_target.inc @@ -92,7 +92,7 @@ # INSTALL_DIR := $(libdir) # -.PHONY: all clean install +.PHONY: all clean install check ifneq ($(strip $(MAKE_TARGETS)),) $(MAKE_TARGETS) += $(HOST_MAKE_TARGETS) @@ -109,4 +109,7 @@ $(INSTALL_FILES): | $(INSTALL_DEPS) install: $(INSTALL_FILES) +$(CHECK_TARGETS): | $(CHECK_DEPS) +check: $(CHECK_TARGETS) + # vim: syntax=make diff --git a/include/mk/lib.mk b/include/mk/lib.mk index f9b6c0aff..a3961bce5 100644 --- a/include/mk/lib.mk +++ b/include/mk/lib.mk @@ -26,6 +26,7 @@ # Makefile to include for libraries. include $(top_srcdir)/include/mk/env_pre.mk +include $(top_srcdir)/include/mk/clang-check.mk INSTALL_DIR := $(libdir) @@ -57,6 +58,8 @@ LIBSRCS := $(filter-out $(FILTER_OUT_LIBSRCS),$(LIBSRCS)) LIBOBJS := $(LIBSRCS:.c=.o) +CHECK_TARGETS := $(addprefix check-,$(notdir $(LIBSRCS:.c=))) + $(LIB): $(notdir $(LIBOBJS)) @if [ -z "$(strip $^)" ] ; then \ echo "Cowardly refusing to create empty archive"; \ diff --git a/include/mk/rules.mk b/include/mk/rules.mk index c8f4bbbbe..2a04b2b67 100644 --- a/include/mk/rules.mk +++ b/include/mk/rules.mk @@ -37,3 +37,12 @@ else @$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $^ $(LTPLDLIBS) $(LDLIBS) -o $@ @echo CC $(target_rel_dir)$@ endif + +.PHONY: $(CHECK_TARGETS) +$(CHECK_TARGETS): check-%: %.c +ifdef VERBOSE + $(CHECK) $(CHECK_FLAGS) $(CPPFLAGS) $(CFLAGS) $< +else + @$(CHECK) $(CHECK_FLAGS) $(CPPFLAGS) $(CFLAGS) $< + @echo CHECK $(target_rel_dir)$< +endif diff --git a/include/mk/testcases.mk b/include/mk/testcases.mk index 1c81773d0..e59899898 100644 --- a/include/mk/testcases.mk +++ b/include/mk/testcases.mk @@ -22,6 +22,7 @@ include $(top_srcdir)/include/mk/env_pre.mk include $(top_srcdir)/include/mk/functions.mk +include $(top_srcdir)/include/mk/clang-check.mk APICMDS_DIR := $(abs_top_builddir)/tools/apicmds diff --git a/tools/clang-check/.gitignore b/tools/clang-check/.gitignore new file mode 100644 index 000000000..ba2906d06 --- /dev/null +++ b/tools/clang-check/.gitignore @@ -0,0 +1 @@ +main diff --git a/tools/clang-check/Makefile b/tools/clang-check/Makefile new file mode 100644 index 000000000..4650d6057 --- /dev/null +++ b/tools/clang-check/Makefile @@ -0,0 +1,14 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# Copyright (c) 2021 SUSE LLC +# Copyright (c) 2019 Cyril Hrubis +# Copyright (c) 2020 Petr Vorel + +top_srcdir ?= ../.. + +include $(top_srcdir)/include/mk/env_pre.mk +include $(top_srcdir)/include/mk/functions.mk + +HOST_MAKE_TARGETS := main +HOST_LDFLAGS += -lclang + +include $(top_srcdir)/include/mk/generic_leaf_target.mk From patchwork Fri Jun 4 11:14:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Palethorpe X-Patchwork-Id: 1487739 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=suse.com header.i=@suse.com header.a=rsa-sha256 header.s=susede1 header.b=gUTls0//; dkim-atps=neutral Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4FxKt10yNXz9sRf for ; Fri, 4 Jun 2021 21:14:49 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id D81C33C4F4E for ; Fri, 4 Jun 2021 13:14:45 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-7.smtp.seeweb.it (in-7.smtp.seeweb.it [217.194.8.7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by picard.linux.it (Postfix) with ESMTPS id 30D3A3C288E for ; Fri, 4 Jun 2021 13:14:44 +0200 (CEST) Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by in-7.smtp.seeweb.it (Postfix) with ESMTPS id 536C320108C for ; Fri, 4 Jun 2021 13:14:43 +0200 (CEST) Received: from relay2.suse.de (unknown [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id D4F711FD4C; Fri, 4 Jun 2021 11:14:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1622805282; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=d7ysqKv1ZoAr4IMQEYbOZnjK3mudcGdrFJEOpb17tAQ=; b=gUTls0//L9jHALI9Ft121wInEr8xavTM2/jAgHF/ywN8Ts7QDBp4l+SO4eYwySJJXJH5ff UoaowAM5SjfSc36tcdAJtS75e8Q2Js5JCD37bTUNfEF4svN8NXz4XhqHO4NiEnIuN+YfWO UXKnQiQL6j/V+cqCC6ASeiMfFVVdRiw= Received: from g78.suse.de (unknown [10.163.24.38]) by relay2.suse.de (Postfix) with ESMTP id A691AA3B89; Fri, 4 Jun 2021 11:14:42 +0000 (UTC) To: ltp@lists.linux.it Date: Fri, 4 Jun 2021 12:14:34 +0100 Message-Id: <20210604111434.21422-3-rpalethorpe@suse.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210604111434.21422-1-rpalethorpe@suse.com> References: <20210604111434.21422-1-rpalethorpe@suse.com> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.102.4 at in-7.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.1 required=7.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on in-7.smtp.seeweb.it Subject: [LTP] [RFC PATCH v2 2/2] Start libclang based analyzer and TEST() check X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Richard Palethorpe via ltp From: Richard Palethorpe Reply-To: Richard Palethorpe Cc: Richard Palethorpe Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" This uses the stable Clang C API to find usages of the TEST() macro. It can also determine if a translation unit is a test executable by finding the struct tst_test test instantiation. This Clang API only exposes the AST along with some other utilities for evaluating constants, indexing, auto completion and source rewriting. This is somewhat less than what Smatch, Coccinelle and the unstable Clang C++ APIs expose. However it is a simple, stable and well supported C API. Signed-off-by: Richard Palethorpe --- tools/clang-check/main.c | 239 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 239 insertions(+) create mode 100644 tools/clang-check/main.c diff --git a/tools/clang-check/main.c b/tools/clang-check/main.c new file mode 100644 index 000000000..26ce898c3 --- /dev/null +++ b/tools/clang-check/main.c @@ -0,0 +1,239 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2021 SUSE LLC + * Copyright (c) 2017 Petr Vorel + * + * Entry point for the LTP static analyser. + * + * Scans the AST (Abstract Syntax Tree) generated by Clang + * twice. First pass we just collect info about the TU (Translation + * Unit). Second pass performs the checks. + * + * AST Nodes are called CXCursor by libclang. We use the library's + * visitor functions to recurse into the AST. + * + * The kind of AST node decides which checks to run on it. + * + * This program takes the same arguments the Clang compiler + * frontend does. Although some are ignored by libclang. + */ +#include +#include +#include +#include +#include + +#define attr_unused __attribute__((unused)) + +/* The rules for test, library and tool code are different */ +enum ltp_tu_kind { + LTP_TEST, + LTP_OTHER, +}; + +/* Holds information about the TU which we gathered on the first pass */ +static struct { + enum ltp_tu_kind tu_kind; +} tu_info; + +static const char *const ansi_red = "\033[1;31m"; +static const char *const ansi_reset = "\033[0m"; +static const char *const ansi_bold = "\033[1m"; + +static unsigned error_flag; + +/* Copied from lib/tst_ansi_color.c */ +static int color_enabled(const int fd) +{ + static int color; + + if (color) + return color - 1; + + const char *const env = getenv("LTP_COLORIZE_OUTPUT"); + + if (env) { + if (!strcmp(env, "n") || !strcmp(env, "0")) + color = 1; + + if (!strcmp(env, "y") || !strcmp(env, "1")) + color = 2; + + return color - 1; + } + + if (isatty(fd) == 0) + color = 1; + else + color = 2; + + return color - 1; +} + +static void emit_error(CXCursor offending_cursor, const char *const error_msg) +{ + CXSourceLocation loc = clang_getCursorLocation(offending_cursor); + CXFile loc_file; + unsigned loc_line, loc_column; + CXString file_name; + + error_flag = 1; + + clang_getFileLocation(loc, &loc_file, &loc_line, &loc_column, + /*offset=*/NULL); + file_name = clang_getFileName(loc_file); + + if (color_enabled(STDERR_FILENO)) { + dprintf(STDERR_FILENO, + "%s:%u:%u: %sCHECK ERROR%s: %s%s%s\n", + clang_getCString(file_name), loc_line, loc_column, + ansi_red, ansi_reset, + ansi_bold, error_msg, ansi_reset); + } else { + dprintf(STDERR_FILENO, + "%s:%u:%u: CHECK ERROR: %s\n", + clang_getCString(file_name), loc_line, loc_column, + error_msg); + } + + clang_disposeString(file_name); +} + +static int cursor_cmp_spelling(const char *const spelling, CXCursor cursor) +{ + CXString cursor_spelling = clang_getCursorSpelling(cursor); + const int ret = strcmp(spelling, clang_getCString(cursor_spelling)); + + clang_disposeString(cursor_spelling); + + return ret; +} + +static int cursor_type_cmp_spelling(const char *const spelling, CXCursor cursor) +{ + CXType ctype = clang_getCursorType(cursor); + CXString ctype_spelling = clang_getTypeSpelling(ctype); + const int ret = strcmp(spelling, clang_getCString(ctype_spelling)); + + clang_disposeString(ctype_spelling); + + return ret; +} + +/* + * Check if the TEST() macro is used inside the library. + * + * This check takes an AST node which should already be known to be a + * macro expansion kind. + * + * If the TU appears to be a test executable then the test does not + * apply. So in that case we return. + * + * If the macro expansion AST node is spelled TEST, then we emit an + * error. Otherwise do nothing. + */ +static void check_TEST_macro(CXCursor macro_cursor) +{ + if (tu_info.tu_kind == LTP_TEST) + return; + + if (!cursor_cmp_spelling("TEST", macro_cursor)) { + emit_error(macro_cursor, + "TEST() macro should not be used in library"); + } +} + +/* Second pass where we run the checks */ +static enum CXChildVisitResult check_visitor(CXCursor cursor, + attr_unused CXCursor parent, + attr_unused CXClientData client_data) +{ + CXSourceLocation loc = clang_getCursorLocation(cursor); + + if (clang_Location_isInSystemHeader(loc)) + return CXChildVisit_Continue; + + switch (clang_getCursorKind(cursor)) { + case CXCursor_MacroExpansion: + check_TEST_macro(cursor); + break; + default: + break; + } + + return CXChildVisit_Recurse; +} + +/* If we find `struct tst_test = {...}` then record that this TU is a test */ +static void info_ltp_tu_kind(CXCursor cursor) +{ + CXCursor initializer; + + if (clang_Cursor_hasVarDeclGlobalStorage(cursor) != 1) + return; + + if (cursor_cmp_spelling("test", cursor)) + return; + + if (cursor_type_cmp_spelling("struct tst_test", cursor)) + return; + + initializer = clang_Cursor_getVarDeclInitializer(cursor); + + if (!clang_Cursor_isNull(initializer)) + tu_info.tu_kind = LTP_TEST; +} + +/* First pass to collect info */ +static enum CXChildVisitResult info_visitor(CXCursor cursor, + attr_unused CXCursor parent, + attr_unused CXClientData client_data) +{ + CXSourceLocation loc = clang_getCursorLocation(cursor); + + if (clang_Location_isInSystemHeader(loc)) + return CXChildVisit_Continue; + + switch (clang_getCursorKind(cursor)) { + case CXCursor_VarDecl: + info_ltp_tu_kind(cursor); + break; + default: + break; + } + + return CXChildVisit_Continue; +} + +int main(const int argc, const char *const *const argv) +{ + CXIndex cindex = clang_createIndex(0, 1); + CXTranslationUnit tu; + CXCursor tuc; + + enum CXErrorCode ret = clang_parseTranslationUnit2( + cindex, + /*source_filename=*/NULL, + argv + 1, argc - 1, + /*unsaved_files=*/NULL, /*num_unsaved_files=*/0, + CXTranslationUnit_DetailedPreprocessingRecord, + &tu); + + if (ret != CXError_Success) { + printf("Failed to load translation unit: %d\n", ret); + return 1; + } + + tuc = clang_getTranslationUnitCursor(tu); + + tu_info.tu_kind = LTP_OTHER; + clang_visitChildren(tuc, info_visitor, NULL); + + clang_visitChildren(tuc, check_visitor, NULL); + + /* Stop leak sanitizer from complaining */ + clang_disposeTranslationUnit(tu); + clang_disposeIndex(cindex); + + return error_flag; +}