[LTP] [PATCH 1/3] tools/sparse: Add static check
Richard Palethorpe
rpalethorpe@suse.com
Tue Nov 23 13:43:46 CET 2021
This was adapted from Sparse's inbuilt check_duplicates (-Wdecl). The
original check appears to print a warning whenever a symbol is
non-static, but has no prototype. It appears to work because library
symbols are usually declared first in a header file and then again
with their definition in a source file.
The LTP version also checks for the various library prefixes, but
should otherwise be the same.
Signed-off-by: Richard Palethorpe <rpalethorpe@suse.com>
---
tools/sparse/sparse-ltp.c | 53 +++++++++++++++++++++++++++++++++++++++
1 file changed, 53 insertions(+)
diff --git a/tools/sparse/sparse-ltp.c b/tools/sparse/sparse-ltp.c
index 45874e8eb..73725d191 100644
--- a/tools/sparse/sparse-ltp.c
+++ b/tools/sparse/sparse-ltp.c
@@ -82,6 +82,57 @@ static void do_entrypoint_checks(struct entrypoint *ep)
} END_FOR_EACH_PTR(bb);
}
+/* Check for LTP-003 and LTP-004
+ *
+ * Try to find cases where the static keyword was forgotten.
+ */
+static void check_symbol_visibility(struct symbol *sym)
+{
+ struct symbol *next = sym;
+ unsigned long mod = sym->ctype.modifiers;
+ const char *name = show_ident(sym->ident);
+ int has_lib_prefix = !strncmp("tst_", name, 4) ||
+ !strncmp("TST_", name, 4) ||
+ !strncmp("ltp_", name, 4) ||
+ !strncmp("safe_", name, 5);
+
+ if (!(mod & MOD_TOPLEVEL))
+ return;
+
+ if (has_lib_prefix && (mod & MOD_STATIC)) {
+ warning(sym->pos,
+ "LTP-003: Symbol '%s' has the LTP public library prefix, but is static (private).",
+ name);
+ return;
+ }
+
+ if ((mod & MOD_STATIC))
+ return;
+
+ if (tu_kind == LTP_LIB && !has_lib_prefix) {
+ warning(sym->pos,
+ "LTP-003: Symbol '%s' is a public library function, but is missing the 'tst_' prefix",
+ name);
+ return;
+ }
+
+ if (next->same_symbol)
+ return;
+
+ if (sym->ident == &main_ident)
+ return;
+
+ warning(sym->pos,
+ "Symbol '%s' has no prototype or library ('tst_') prefix. Should it be static?",
+ name);
+}
+
+/* AST level checks */
+static void do_symbol_checks(struct symbol *sym)
+{
+ check_symbol_visibility(sym);
+}
+
/* Compile the AST into a graph of basicblocks */
static void process_symbols(struct symbol_list *list)
{
@@ -90,6 +141,8 @@ static void process_symbols(struct symbol_list *list)
FOR_EACH_PTR(list, sym) {
struct entrypoint *ep;
+ do_symbol_checks(sym);
+
expand_symbol(sym);
ep = linearize_symbol(sym);
if (!ep || !ep->entry)
--
2.33.1
More information about the ltp
mailing list