Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iwyu: add map file to correctly and deterministicly lint includes #2837

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .iwyu.imp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

[
# json-c: ensure compatibility for multiple versions, only include public facade header json.h
{ "include": [ "\"json_object.h\"", "private", "<json.h>", "public"] },
{ "include": [ "\"json_tokener.h\"", "private", "<json.h>", "public"] },
{ "include": [ "\"json_pointer.h\"", "private", "<json.h>", "public"] },
{ "include": [ "\"json_object_iterator.h\"", "private", "<json.h>", "public"] },
{ "include": [ "\"json_visit.h\"", "private", "<json.h>", "public"] },
{ "include": [ "\"json_util.h\"", "private", "<json.h>", "public"] },
{ "include": [ "\"json_types.h\"", "private", "<json.h>", "public"] },
# cmocka: cmocka.h requires several stdlib headers, encapsulated in cmocka_all.h
{ "include": [ "<cmocka.h>", "private", "\"../helper/cmocka_all.h\"", "public"] },
# stdlib:
{ "include": [ "<bits/mman-shared.h>", "private", "<sys/mman.h>", "public"] },
{ "include": [ "<bits/types/mbstate_t.h>", "private", "<wchar.h>", "public"] },
# crypto: we have an internal wrapper to abstract openssl/mbedtls/...
{ "include": [ "\"esys_crypto_ossl.h\"", "private", "\"esys_crypto.h\"", "public"] },
{ "include": [ "\"bits/mman-shared.h\"", "private", "\"esys_crypto.h\"", "public"] },
# openssl: prevent indirect import of <openssl/ocsp.h>
{ "symbol": [ "d2i_X509", "private", "<openssl/x509.h>", "public"] },
{ "symbol": [ "X509_free", "private", "<openssl/x509.h>", "public"] },
{ "symbol": [ "X509_get_pubkey", "private", "<openssl/x509.h>", "public"] },
{ "symbol": [ "X509_REQ_free", "private", "<openssl/x509.h>", "public"] },
{ "symbol": [ "X509_STORE_add_cert", "private", "<openssl/x509.h>", "public"] },
# openssl: <openssl/types.h> was introduced in v3.0, use <openssl/evp.h> instead
{ "include": [ "<openssl/types.h>", "private", "<openssl/evp.h>", "public"] },
]
184 changes: 92 additions & 92 deletions Makefile-test.am

Large diffs are not rendered by default.

48 changes: 7 additions & 41 deletions doc/coding_standard_c.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,53 +280,19 @@ These formatting conditions are contrary to Kernighan and Ritchie's "one true br
In order to keep a clear include section in all files, we utilize the
include-what-you-use tool.

Since json-c changed their header-layout throughout its versions, we will
just include json-c/json.h with a IWYU pragma keep.
Also all occurrences of config.h had to be tagged with a keep pragma.
Further, openssl/types.h was only introduced with OpenSSL 3.0 but is covered
by openssl/evp.h in most cases.

Given this, a lot of manual rework needs to be done after an iwyu run.
Thus, we do not require it at the moment, but will run in every once in
a while.
```sh
# Go for the .c files and .h files with corresponding .c files
./bootstrap
./configure --enable-integration --enable-unit
make clean
make -k CC=include-what-you-use CXXFLAGS="-Xixyu" 2>&1 | tee iwyu-changes
make -k CC=include-what-you-use CXXFLAGS="-Xixyu" check-programs 2>&1 | tee -a iwyu-changes

# Go to headers without corresponding .c files
for i in $(find src -iname '*.h')
do
base=${i::-2}
if test ! -f $base.c
then
include-what-you-use -I. -I./include/tss2 -I./src $base.h
fi
done 2>&1 | tee -a iwyu-changes

# Apply the fixes
fix_include --comments --update_comments --reorder --nosafe_headers <iwyu-changes

# For some reason, IWYU has problems with the json-c headers. Thus, we fix this as follows.
find \( -iname *.c -or -iname '*.h' \) -exec sed 's/\"json_object.h\"/<json-c\/json_object.h>/' -i {} \;
find \( -iname *.c -or -iname '*.h' \) -exec sed 's/\"json_types.h\"/<json-c\/json_types.h>/' -i {} \;
find \( -iname *.c -or -iname '*.h' \) -exec sed 's/\"json_tokener.h\"/<json-c\/json_tokener.h>/' -i {} \;
find \( -iname *.c -or -iname '*.h' \) -exec sed 's/\"linkhash.h\"/<json-c\/linkhash.h>/' -i {} \;
find test/unit test/integration \( -iname *.c -or -iname '*.h' \) -exec sed 's/cmocka.h/cmocka_all.h/' -i {} \;

# From here we need to go back to the make clean step and go a few rounds of the .c files until we have a compile-ready thing.
make clean
make -k CC=include-what-you-use CXXFLAGS="-Xixyu" 2>&1 | tee iwyu-changes
make -k CC=include-what-you-use CXXFLAGS="-Xixyu" check-programs 2>&1 | tee -a iwyu-changes
fix_include --comments --update_comments --reorder --nosafe_headers <iwyu-changes

# There will still be compiler errors, since IWYU does not detect the json_object_object_foreach makro.
# In those cases, the json_object.h include needs to be added manually.
# Clean up .c files and their corresponding .h files
make -j -k CC=include-what-you-use CFLAGS="-Xiwyu --mapping_file=.iwyu.imp" check-programs 3>&1 1>&2 2>&3 | fix_include --comments --update_comments --reorder --nosafe_headers

# Work also on headers (without a corresponding .c file)
find src -iname '*.h' | xargs -n1 -P$(nproc) include-what-you-use -Xiwyu --mapping_file=.iwyu.imp -I. -I./src -I./include/tss2 -I./test/fuzz/tcti -I./include -I./src/tss2-mu -I./src/tss2-sys -I./src/tss2-esys -I./src/tss2-fapi -I./test/data -I/usr/include/json-c 2>&1 | fix_include --comments --update_comments --reorder --nosafe_headers

# TODO: go for make check-programs
# Make sure that iwyu did not break anything
make -j check
```

## References
Expand Down
5 changes: 2 additions & 3 deletions src/tss2-esys/esys_crypto_ossl.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "config.h" // for HAVE_EVP_SM4_CFB
#endif

#include <inttypes.h> // for uint8_t, PRIu16
#include <inttypes.h> // for PRIu16
#include <openssl/bn.h> // for BN_free, BN_bin2bn, BN_bn2bin, BN_n...
#include <openssl/crypto.h> // for OSSL_LIB_CTX_free, OSSL_LIB_CTX_new
#include <openssl/ec.h> // for EC_POINT_free, EC_POINT_new, EC_GRO...
Expand All @@ -25,9 +25,8 @@
#include <openssl/core_names.h> // for OSSL_PKEY_PARAM_EC_PUB_X, OSSL_PKEY...
#include <openssl/param_build.h> // for OSSL_PARAM_BLD_free, OSSL_PARAM_BLD...
#include <openssl/params.h> // for OSSL_PARAM_free
#include <openssl/types.h> // for BIGNUM, EVP_CIPHER, EVP_CIPHER_CTX
#endif
#include "esys_crypto.h" // for iesys_crypto_hash_get_digest_size
#include "esys_crypto.h" // for OSSL_FREE, iesys_crypto_hash_get_di...
#include "esys_crypto_ossl.h"
#include "tss2_esys.h" // for ESYS_CRYPTO_CONTEXT_BLOB
#include "tss2_mu.h" // for Tss2_MU_TPMS_ECC_POINT_Marshal
Expand Down
2 changes: 1 addition & 1 deletion src/tss2-fapi/api/Fapi_ExportKey.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "config.h" // IWYU pragma: keep
#endif

#include <json-c/json.h> // for json_object, json_object_put, json_object_to_js...
#include <json.h> // for json_object_put, json_object_to_js...
#include <stdlib.h> // for NULL
#include <string.h> // for memset, strdup

Expand Down
2 changes: 1 addition & 1 deletion src/tss2-fapi/api/Fapi_ExportPolicy.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#endif

#include <inttypes.h> // for PRIu16
#include <json-c/json.h> // for json_object, json_object_put, json_object_to_js...
#include <json.h> // for json_object_to_json_string_ext
#include <stdbool.h> // for false, true
#include <string.h> // for memset, size_t, NULL

Expand Down
2 changes: 1 addition & 1 deletion src/tss2-fapi/api/Fapi_GetInfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "config.h" // for PACKAGE_STRING
#endif

#include <json-c/json.h> // for json_object, json_object_put, json_object_to_js...
#include <json.h> // for json_object_put, json_object_to_js...
#include <string.h> // for memset, strdup, NULL, size_t

#include "fapi_int.h" // for FAPI_CONTEXT, IFAPI_GetInfo, IFAPI...
Expand Down
2 changes: 1 addition & 1 deletion src/tss2-fapi/api/Fapi_GetTpmBlobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "config.h" // IWYU pragma: keep
#endif

#include <json-c/json.h> // for json_object, json_object_put, json_object_to_js...
#include <json.h> // for json_object_put, json_objec...
#include <stdint.h> // for uint8_t
#include <stdlib.h> // for malloc, size_t, NULL
#include <string.h> // for memcpy
Expand Down
2 changes: 1 addition & 1 deletion src/tss2-fapi/api/Fapi_Import.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "config.h" // IWYU pragma: keep
#endif

#include <json-c/json.h> // for json_object, json_object_put, json_object_to_js...
#include <json.h> // for json_object_put, json_tok...
#include <stdint.h> // for uint16_t
#include <stdlib.h> // for NULL, size_t
#include <string.h> // for strncmp, memset, memcpy
Expand Down
2 changes: 1 addition & 1 deletion src/tss2-fapi/api/Fapi_NvExtend.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "config.h" // IWYU pragma: keep
#endif

#include <json-c/json.h> // for json_object, json_object_put, json_object_to_js...
#include <json.h> // for json_object_array_add, json_object...
#include <stdint.h> // for uint8_t
#include <stdlib.h> // for NULL, malloc, size_t
#include <string.h> // for memcpy, memset
Expand Down
2 changes: 1 addition & 1 deletion src/tss2-fapi/api/Fapi_NvIncrement.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "config.h" // IWYU pragma: keep
#endif

#include <json-c/json.h> // for json_object, json_object_put, json_object_to_js...
#include <json.h> // for json_object
#include <stdlib.h> // for NULL
#include <string.h> // for memset

Expand Down
2 changes: 1 addition & 1 deletion src/tss2-fapi/api/Fapi_NvSetBits.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#endif

#include <inttypes.h> // for uint64_t, PRIx64
#include <json-c/json.h> // for json_object, json_object_put, json_object_to_js...
#include <json.h> // for json_object
#include <stdlib.h> // for NULL
#include <string.h> // for memset

Expand Down
2 changes: 1 addition & 1 deletion src/tss2-fapi/api/Fapi_NvWrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "config.h" // IWYU pragma: keep
#endif

#include <json-c/json.h> // for json_object, json_object_put, json_object_to_js...
#include <json.h> // for json_object
#include <stdint.h> // for uint8_t
#include <stdlib.h> // for NULL, malloc, size_t
#include <string.h> // for memcpy, memset
Expand Down
2 changes: 1 addition & 1 deletion src/tss2-fapi/api/Fapi_VerifyQuote.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "config.h" // IWYU pragma: keep
#endif

#include <json-c/json.h> // for json_object, json_object_put, json_object_to_js...
#include <json.h> // for json_object_put, json_tokener_parse
#include <stdint.h> // for uint8_t
#include <stdlib.h> // for NULL, size_t, malloc
#include <string.h> // for memset, memcmp, memcpy
Expand Down
3 changes: 1 addition & 2 deletions src/tss2-fapi/fapi_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <openssl/buffer.h> // for buf_mem_st
#include <openssl/crypto.h> // for OSSL_LIB_CTX_free, OSSL_LIB_CTX_new
#include <openssl/ec.h> // for ECDSA_SIG_free, i2d_ECDSA_SIG, ECDS...
#include <openssl/evp.h> // for EVP_PKEY_type, EVP_PKEY_free, EVP_P...
#include <openssl/evp.h> // for EVP_PKEY_type, EVP_PKEY_free, EVP_PKEY
#include <openssl/obj_mac.h> // for NID_sm2, NID_X9_62_prime192v1, NID_...
#include <openssl/objects.h> // for OBJ_nid2sn, OBJ_txt2nid
#include <openssl/opensslv.h> // for OPENSSL_VERSION_NUMBER
Expand All @@ -31,7 +31,6 @@
#include <openssl/core_names.h> // for OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PK...
#include <openssl/param_build.h> // for OSSL_PARAM_BLD_free, OSSL_PARAM_BLD...
#include <openssl/params.h> // for OSSL_PARAM_free
#include <openssl/types.h> // for EVP_PKEY, BIGNUM, BIO, EVP_PKEY_CTX
#endif
#include <openssl/err.h> // for ERR_print_errors_fp

Expand Down
2 changes: 1 addition & 1 deletion src/tss2-fapi/fapi_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#ifndef FAPI_INT_H
#define FAPI_INT_H

#include <json-c/json.h> // for json_object, json_object_put, json_object_to_js...
#include <json.h> // for json_object
#include <stdbool.h> // for bool, false, true
#include <stdint.h> // for uint8_t, uint32_t, uint64_t
#include <stdio.h> // for size_t, NULL
Expand Down
2 changes: 1 addition & 1 deletion src/tss2-fapi/fapi_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "config.h" // IWYU pragma: keep
#endif

#include <json-c/json.h> // for json_object, json_object_put, json_object_to_js...
#include <json.h> // for json_object_put, json_object_t...
#include <stdlib.h> // for free, calloc, malloc
#include <string.h> // for memcpy, strcmp, memset, strlen
#include <strings.h> // for strcasecmp
Expand Down
2 changes: 1 addition & 1 deletion src/tss2-fapi/ifapi_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "config.h" // for SYSCONFDIR
#endif

#include <json-c/json.h> // for json_object, json_object_put, json_object_to_js...
#include <json.h> // for json_object_put, json_object
#include <stdint.h> // for uint8_t
#include <stdlib.h> // for NULL, getenv, size_t
#include <string.h> // for strncmp, strlen, memset, strdup
Expand Down
5 changes: 2 additions & 3 deletions src/tss2-fapi/ifapi_curl.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
#endif

#include <curl/curl.h> // for curl_easy_strerror, CURLE_OK, curl_ea...
#include <openssl/asn1.h> // for asn1_string_st
#include <openssl/bio.h> // for BIO_free, BIO_new_mem_buf
#include <openssl/evp.h> // for X509, ASN1_IA5STRING, X509_CRL, DIST_...
#include <openssl/obj_mac.h> // for NID_crl_distribution_points, NID_info...
#include <openssl/opensslv.h> // for OPENSSL_VERSION_NUMBER
#include <openssl/pem.h> // for PEM_read_bio_X509
#include <openssl/safestack.h> // for STACK_OF
#include <openssl/x509.h> // for X509_free, X509_STORE_add_cert, X509_...
Expand All @@ -17,8 +18,6 @@

#if OPENSSL_VERSION_NUMBER < 0x30000000L
#include <openssl/aes.h>
#else
#include <openssl/types.h> // for X509, ASN1_IA5STRING, X509_CRL, DIST_...
#endif

#include "fapi_certificates.h" // for root_cert_list
Expand Down
5 changes: 2 additions & 3 deletions src/tss2-fapi/ifapi_eventlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "config.h" // IWYU pragma: keep
#endif

#include <json-c/json.h> // for json_object, json_object_put, json_object_to_js...
#include <stdint.h> // for uint8_t
#include <stdlib.h> // for free
#include <string.h> // for memset, strdup, memcpy
Expand All @@ -18,9 +17,9 @@
#include "ifapi_eventlog.h"
#include "ifapi_helpers.h" // for ifapi_asprintf, ifapi_cal...
#include "ifapi_ima_eventlog.h" // for IFAPI_IMA_EVENT, ifapi_re...
#include "ifapi_json_deserialize.h" // for ifapi_json_IFAPI_EVENT_deserialize
#include "ifapi_json_serialize.h" // for ifapi_json_IFAPI_EVENT_se...
#include "ifapi_json_deserialize.h" // for ifapi_json_IFAPI_EVENT_de...
#include "ifapi_json_eventlog_serialize.h" // for ifapi_get_tcg_firmware_ev...
#include "ifapi_json_serialize.h" // for ifapi_json_IFAPI_EVENT_se...
#include "ifapi_macros.h" // for check_not_null, statecase
#include "tpm_json_deserialize.h" // for ifapi_parse_json

Expand Down
2 changes: 1 addition & 1 deletion src/tss2-fapi/ifapi_eventlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#ifndef IFAPI_EVENTLOG_H
#define IFAPI_EVENTLOG_H

#include <json-c/json.h> // for json_object, json_object_put, json_object_to_js...
#include <json.h> // for json_object
#include <stdbool.h> // for bool
#include <stddef.h> // for size_t

Expand Down
1 change: 0 additions & 1 deletion src/tss2-fapi/ifapi_eventlog_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#endif

#include <inttypes.h> // for uintptr_t, uint8_t, int64_t, PRId64
#include <json-c/json.h> // for json_object, json_object_put, json_object_to_js...
#include <stdio.h> // for sscanf
#include <stdlib.h> // for calloc
#include <string.h> // for strlen, strncmp, memcmp, strcmp
Expand Down
12 changes: 6 additions & 6 deletions src/tss2-fapi/ifapi_eventlog_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
#ifndef IFAPI_EVENTLOG_SYSTEM_H
#define IFAPI_EVENTLOG_SYSTEM_H

#include <json-c/json.h> // for json_object, json_object_put, json_object_to_js...
#include <stdbool.h> // for bool
#include <stddef.h> // for size_t
#include <json.h> // for json_object
#include <stdbool.h> // for bool
#include <stddef.h> // for size_t

#include "efi_event.h" // for TCG_EVENT_HEADER2, TCG_DIGEST2, TCG_E...
#include "fapi_types.h" // for UINT8_ARY
#include "tss2_common.h" // for UINT32, BYTE, TSS2_RC
#include "efi_event.h" // for TCG_EVENT_HEADER2, TCG_DIGEST2, TCG_EVENT
#include "fapi_types.h" // for UINT8_ARY
#include "tss2_common.h" // for UINT32, BYTE, TSS2_RC

typedef UINT32 TCG_EVENT_TYPE;

Expand Down
5 changes: 2 additions & 3 deletions src/tss2-fapi/ifapi_get_web_cert.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
#endif

#include <curl/curl.h> // for curl_easy_cleanup, curl_easy_init
#include <json-c/json.h> // for json_object, json_object_put, json_object_to_js...
#include <json.h> // for json_object_get_string, json_objec...
#include <openssl/bio.h> // for BIO_new, BIO_free_all, BIO_push
#include <openssl/buffer.h> // for buf_mem_st
#include <openssl/evp.h> // for EVP_DigestUpdate, BIO_f_base64
#include <openssl/opensslv.h> // for OPENSSL_VERSION_NUMBER
#include <openssl/sha.h> // for SHA256_DIGEST_LENGTH
#include <stdbool.h> // for bool, true
#include <stdint.h> // for uint8_t
Expand All @@ -18,8 +19,6 @@

#if OPENSSL_VERSION_NUMBER < 0x30000000L
#include <openssl/aes.h>
#else
#include <openssl/types.h> // for X509, ASN1_IA5STRING, X509_CRL, DIST_...
#endif

#include "fapi_int.h" // for FAPI_CONTEXT, VENDOR_AMD, VENDOR_INTC
Expand Down
2 changes: 1 addition & 1 deletion src/tss2-fapi/ifapi_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include <errno.h> // for EEXIST, errno
#include <inttypes.h> // for PRIu16, SCNx32, PRIi32, PRIu32
#include <json-c/json.h> // for json_object, json_object_put, json_object_to_js...
#include <stdarg.h> // for va_list, va_end, va_copy, va_start
#include <stdio.h> // for sscanf, vsnprintf, vsprintf, vas...
#include <stdlib.h> // for malloc, calloc, free
Expand All @@ -27,6 +26,7 @@
#include "ifapi_json_serialize.h" // for ifapi_json_FAPI_QUOTE_INFO_seria...
#include "ifapi_macros.h" // for strdup_check, goto_if_error2
#include "ifapi_policy.h" // for ifapi_compute_policy_digest
#include "linkhash.h" // for lh_entry
#include "tpm_json_deserialize.h" // for ifapi_parse_json
#include "tss2_mu.h" // for Tss2_MU_TPMI_ALG_HASH_Marshal

Expand Down
2 changes: 1 addition & 1 deletion src/tss2-fapi/ifapi_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#ifndef IFAPI_HELPERS_H
#define IFAPI_HELPERS_H

#include <json-c/json.h> // for json_object, json_object_put, json_object_to_js...
#include <json.h> // for json_object
#include <stdbool.h> // for bool
#include <stddef.h> // for size_t
#include <stdint.h> // for uint8_t
Expand Down
3 changes: 1 addition & 2 deletions src/tss2-fapi/ifapi_ima_eventlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
#endif

#include <inttypes.h> // for PRIu32
#include <json-c/json.h> // for json_object, json_object_put, json_object_to_js...
#include <openssl/evp.h> // for EVP_MD, EVP_get_digestbyname, EVP_MD_size
#include <openssl/evp.h> // for EVP_get_digestbyname, EVP_MD, EVP_...
#include <stdbool.h> // for bool, false, true
#include <stdio.h> // for fread, fclose, FILE, fopen, sprintf
#include <stdlib.h> // for calloc, malloc
Expand Down
Loading
Loading