Skip to content

Instantly share code, notes, and snippets.

@steipete
Last active March 13, 2018 14:47
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save steipete/86c4db2cda22aa7427bb453907885c1f to your computer and use it in GitHub Desktop.
Save steipete/86c4db2cda22aa7427bb453907885c1f to your computer and use it in GitHub Desktop.
Did you knew that Clang Analyzer as alpha checkers? Early Christmas is here! (Ignore the rest... we run our PDF SDK https://pspdfkit.com with -Weverything because warnings are awesome to prevent bugs) - See https://gist.github.com/steipete/28849365e603dc2015c7107d85142e7b/revisions for a list of Xcode 8.3 changes
// clang -cc1 -analyzer-checker-help
// OVERVIEW: Clang Static Analyzer Checkers List
// USAGE: -analyzer-checker <CHECKER or PACKAGE,...>
//
// CHECKERS:
// alpha.core.BoolAssignment Warn about assigning non-{0,1} values to Boolean variables
// alpha.core.CallAndMessageUnInitRefArg
// Check for logical errors for function calls and Objective-C message expressions (e.g., uninitialized arguments, null function pointers, and pointer to undefined variables)
// alpha.core.CastSize Check when casting a malloc'ed type T, whether the size is a multiple of the size of T
// alpha.core.CastToStruct Check for cast from non-struct pointer to struct pointer
// alpha.core.DynamicTypeChecker Check for cases where the dynamic and the static type of an object are unrelated.
// alpha.core.FixedAddr Check for assignment of a fixed address to a pointer
// alpha.core.IdenticalExpr Warn about unintended use of identical expressions in operators
// alpha.core.PointerArithm Check for pointer arithmetic on locations other than array elements
// alpha.core.PointerSub Check for pointer subtractions on two pointers pointing to different memory chunks
// alpha.core.SizeofPtr Warn about unintended use of sizeof() on pointer expressions
// alpha.core.TestAfterDivZero Check for division by variable that is later compared against 0. Either the comparison is useless or there is division by zero.
// alpha.cplusplus.VirtualCall Check virtual function calls during construction or destruction
// alpha.deadcode.UnreachableCode Check unreachable code
// alpha.osx.cocoa.DirectIvarAssignment
// Check for direct assignments to instance variables
// alpha.osx.cocoa.DirectIvarAssignmentForAnnotatedFunctions
// Check for direct assignments to instance variables in the methods annotated with objc_no_direct_instance_variable_assignment
// alpha.osx.cocoa.InstanceVariableInvalidation
// Check that the invalidatable instance variables are invalidated in the methods annotated with objc_instance_variable_invalidator
// alpha.osx.cocoa.MissingInvalidationMethod
// Check that the invalidation methods are present in classes that contain invalidatable instance variables
// alpha.osx.cocoa.localizability.PluralMisuseChecker
// Warns against using one vs. many plural pattern in code when generating localized strings.
// alpha.security.ArrayBound Warn about buffer overflows (older checker)
// alpha.security.ArrayBoundV2 Warn about buffer overflows (newer checker)
// alpha.security.MallocOverflow Check for overflows in the arguments to malloc()
// alpha.security.ReturnPtrRange Check for an out-of-bound pointer being returned to callers
// alpha.security.taint.TaintPropagation
// Generate taint information used by other checkers
PSPDF_ANALYZER_FLAGS = -Xclang -analyzer-checker -Xclang alpha.core.BoolAssignment -Xclang -analyzer-checker -Xclang alpha.core.CallAndMessageUnInitRefArg -Xclang -analyzer-checker -Xclang alpha.core.DynamicTypeChecker -Xclang -analyzer-checker -Xclang alpha.core.FixedAddr -Xclang -analyzer-checker -Xclang alpha.core.IdenticalExpr -Xclang -analyzer-checker -Xclang alpha.core.PointerSub -Xclang -analyzer-checker -Xclang alpha.core.SizeofPtr -Xclang -analyzer-checker -Xclang alpha.core.TestAfterDivZero -Xclang -analyzer-checker -Xclang alpha.security.ArrayBoundV2 -Xclang -analyzer-checker -Xclang alpha.security.MallocOverflow -Xclang -analyzer-checker -Xclang alpha.security.ReturnPtrRange -Xclang -analyzer-checker -Xclang alpha.security.taint.TaintPropagation
// Too noisy:
// -Xclang -analyzer-checker -Xclang alpha.core.CastSize
// -Xclang -analyzer-checker -Xclang alpha.cplusplus.VirtualCall
// -Xclang -analyzer-checker -Xclang alpha.core.PointerArithm
// -Xclang -analyzer-checker -Xclang alpha.core.CastToStruct
// -Xclang -analyzer-checker -Xclang alpha.deadcode.UnreachableCode
// -Xclang -analyzer-checker -Xclang alpha.osx.cocoa.localizability.PluralMisuseChecker
PSPDF_WARNINGS_CFLAGS = $(inherited) $(PSPDF_ANALYZER_FLAGS) -Weverything -Wno-error-deprecated-declarations -Wno-error-deprecated-implementations -Wno-objc-missing-property-synthesis -Wno-unused-parameter -Wno-covered-switch-default -Wno-direct-ivar-access -Wno-assign-enum -Wno-float-equal -Wno-vla -Wno-documentation-unknown-command -Wno-packed -Wno-padded -Wno-auto-import -Wno-selector -Wno-sign-conversion -Wno-auto-import -Wno-static-in-inline -Wno-gnu-conditional-omitted-operand -Wno-gnu-zero-variadic-macro-arguments -Wno-gnu-statement-expression -Wno-language-extension-token -Wno-pointer-arith -Wno-empty-translation-unit -Wno-format-non-iso -Wno-comment -Wno-gnu-folding-constant -Wno-c++98-compat-pedantic -Wno-c++98-compat -Wno-old-style-cast -Wno-incomplete-module -Wno-vla-extension -Wno-c99-extensions -Wno-cstring-format-directive -Rno-module-build -Wno-reserved-id-macro -Wno-undef -Wno-weak-vtables -Wno-over-aligned -Wno-double-promotion -Wno-incompatible-sysroot -Wno-gnu-auto-type
WARNING_CFLAGS = $(PSPDF_WARNINGS_CFLAGS)
@nesterenkodm
Copy link

also how did you manage to avoid -Wno-nullable-to-nonnull-conversion?

@steipete
Copy link
Author

steipete commented Feb 10, 2017

@chebur We use asserting casts in PSPDFKit

template <typename T>
inline T _Nonnull nn(T _Nullable o) noexcept {
    assert(o && "Object must exist");
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnullable-to-nonnull-conversion"
    // We cannot explicitely cast, as this generates another error:
    // error: explicit ownership qualifier on cast result has no effect
    // Since this is just compiler sugar, it doesn't matter.
    return o;
#pragma clang diagnostic pop
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment