set(CTEST_CUSTOM_MAXIMUM_NUMBER_OF_ERRORS 1000)
set(CTEST_CUSTOM_MAXIMUM_NUMBER_OF_WARNINGS 1000)

list(APPEND CTEST_CUSTOM_MEMCHECK_IGNORE
  # Issues in third party glut library
  VTK::RenderingExternalCxx-TestGLUTRenderWindow
  VTK::IOVPICCxx-TestVPICReader
  VTK::RenderingFreeTypeFontConfigCxx-TestSystemFontRendering
  VTK::GeovisGDALCxx-TestRasterReprojectionFilter

  # The PLY code does out of range ops but that is just how
  # it is architected currently. It stores every value into
  # uint, int, and double but typically only uses the
  # value that makes sense for the type read in. Code needs
  # to be reworked eventually but the out of range values
  # are likely not being used.
  VTK::IOPLYCxx-TestPLYWriter
  VTK::IOPLYCxx-TestPLYWriterString
  VTK::IOPLYCxx-TestPLYReaderTextureUVPoints
  VTK::IOPLYCxx-TestPLYReaderTextureUVFaces

  # TestLinePlotDouble intentionally uses very large doubles
  # even though the implementation of the class is largely
  # float. Fixing this requires reworking a few classes to
  # use doubles everywhere (which they generally should)
  # when that is done this can be removed.
  # The key classes impacted are vtkPlotPoints and the
  # Draw calls in Context2D
  VTK::ChartsCoreCxx-TestLinePlotDouble

  # loguru looks like it leaks the thread name but apparently
  # it eventually does free the memory. See
  # https://github.com/emilk/loguru/issues/132
  # for some reason only this test seems to report it
  VTK::FiltersParallelDIY2Cxx-TestRedistributeDataSetFilter
)

list(APPEND CTEST_CUSTOM_WARNING_MATCH
  "{standard input}:[0-9][0-9]*: Warning: ")

list(APPEND CTEST_CUSTOM_WARNING_EXCEPTION
  # classes that have been marked deprecated
  # and will be removed in the future
  # To be remove din VTK 9.0
  "vtkTemporalStreamTracer"

  # Java compilation warnings that don't matter.
  "^1 warning$"
  "bootstrap class path not set in conjunction with -source"

  # OSX has deprecated openGL but we still use it
  # and probably will not update the code but replace it
  # with vulklan/moltenVK in the future
  "Cocoa.*deprecated"
  "NSOpenGL.*deprecated"

  # C4275 carries with it a note that we don't care about. std::exception is
  # the main culprit here.
  "vcruntime_exception.h.*: note: see declaration of 'std::exception'"

  # C4702 warning (unreachable code) caused by intentional abort() used
  # in test which prevents the return statement from being called.
  "TestLoggerDisableSignalHandler.*unreachable code"

  # GetVersion is deprecated, but its use is OK.
  "sysinfoapi.h.*: note: see declaration of 'GetVersion'"

  # Suppress notes from template instantiation backtraces.
  "note: see reference to (class|function) template instantiation"
  "note: while compiling class template member function"

  # This is secondary output from clang, not indicating the warning per se.
  "[0-9]* warnings? generated"
  "note: \\(skipping [0-9]* expansions in backtrace"
  "note: expanded from (here|macro)"

  # This is secondary output from MSVC, not indicate the warning per se.
  "note: see declaration of"
  "note: see previous definition of"

  # Ignore macro expansion lines from GCC
  "note: in expansion of macro"

  # function cast in vtkLogger/loguru
  "vtkLogger.cxx.*: warning: cast between incompatible function types"

  # Qt headers cause C4127 warnings with MSVC. Nothing we can do to fix them.
  "[Qq]t([Cc]ore|[Gg]ui|[Tt]est|[Ww]idgets).*(warning|note)"
  # Qt has some functions marked as `__forceinline` which MSVC refuses to inline.
  "[Qq]t.*warning C4714"
  "[Qq]t.*note: see declaration of"

  # Python notes triggered from MPI4Py Cython-generated code.
  "include/python3.*: note:"

  # macOS warnings about dependencies htat are newer than the CI target
  # version.
  "ld: warning: dylib \\(/Applications/Xcode.*\\.app/Contents/Developer/Library/Frameworks/.*\\) was built for newer macOS version \\(.*\\) than being linked \\(.*\\)"

  # Warning notes from Xcode.
  "/Applications/Xcode.*: note:"

  # Ignore moc-generated code (and rcc and uic).
  "\\.dir[/\\\\][^/\\\\]*_autogen"

  # Intel compilers warn about large functions, but we don't usually care.
  "remark #11074: Inlining inhibited by limit (max-total-size|max-size)"
  # Ignore the suggestion line for more information too.
  "remark #11076: To get full report use"

  # clang-tidy ignores
  "vtkParse.tab.c.*bugprone-sizeof-expression"
  "vtkParse.tab.c.*bugprone-suspicious-include"
  "mocs_compilation.cpp.*bugprone-suspicious-include"
  "vtkkwiml/test/test_.*: warning: .* \\[modernize-redundant-void-arg\\]"

  # Ignore warnings from the standard library.
  "warning:.*__builtin_memcpy.*stringop-overflow="
  "note: at offset .* into destination object"

  # Ignore some tidy warnings from wrapper code.
  "vtk[^\\.]+(Java|Python).cxx:.*\\[(readability-redundant-string-init)\\]"

  # Ignore some doxygen warnings
  "warning: documented symbol 'template class .*' was not declared or defined."
  "vtkContext2D.h:.*: warning: argument '.*' of command @param is not found in the argument list of vtkContext2D::DrawMarkers"
  "vtkMultiThreshold.h:.*: warning: argument '.*' of command @param is not found in the argument list of vtkMultiThreshold::AddIntervalSet"
  "QQuickVTKRenderWindow.h:.*: warning: QQuickVTKRenderWindow::captureScreenshot has @param documentation sections but no arguments"
  "vtkInherits.h:.*: warning: documented symbol 'static vtk::ParentClasses.*::value' was not declared or defined."
  "vtkPolyhedron.h:.*: warning: vtkPolyhedron::GetCellFaces has @param documentation sections but no arguments"

  # Ignore bits from gcc-12 stdlib. There seem to be some serious issues in GCC
  # 12 diagnostics. These come from the `quay.io/pypa/manylinux_2_28_x86_64`
  # container as of now.
  "/opt/rh/gcc-toolset-12"

  # GCC 10.1 contains a bugfix for an error in previous GCC versions that
  # resulted in a class zero-sized subobject to be passed incorrectly when
  # compiled as C++17 or C++20 in versions of GCC prior to 10.1. GCC 10.1 fixes
  # this by forcing the C++14 implementation and issuing a warning.
  "when C\\+\\+17 is enabled changed to match C\\+\\+14 in GCC 10.1"
  )

set(cdash_show_third_party_warnings "OFF")
if (NOT cdash_show_third_party_warnings)
  list(APPEND CTEST_CUSTOM_WARNING_EXCEPTION
    # issue with exodus but exodus is not included in the note
    # should these issues get fixed in exodus then remove this
    # suppression
    "note: '__builtin___snprintf_chk'"

    # HDF5 lex/yacc sources compilation lacks the "ThirdParty" part of the path.
    "hl/src/H5LT(parse|analyze)"

    # Suppress ThirdParty source code from displaying warnings.
    "[Tt]hird[Pp]arty"

    # Suppress MetaIO warnings.
    "MetaIO/vtkmetaio"

    # clang will often give multiline warnings from macro expansions,
    # where the first part has "warning:" and subsequent parts have "note:"
    # and those will sometimes expand into systems headers. When this occurs
    # for ThirdParty code, we want it suppressed too. Do so here for headers within Xcode.
    "Xcode\\.app/Contents/Developer.+note:"

    # Suppress Remote module source code from displaying warnings.
    # Suppress modules individually as just "Remote" is a common pattern
    "[Rr]emote.[Mm]oment[Ii]nvariants"
    "[Rr]emote.[Pp]oisson[Rr]econstruction"
    "[Rr]emote.[Pp]owercrust"

    # sometimes we use system third party headers with issues
    # in this case liblas
    "include/liblas"

    # in this case a redefinition of strndup between netcdf and
    # /usr/include/x86_64-linux-gnu/bits/string2.h  but the warning
    # does not include ThirdParty in the line
    "bits/string2\\.h"

    # some windows link warnings related to hdf5 that do not include
    # ThirdParty in the message
    "H5.*\\.c\\.obj : warning LNK4221"

    # ThirdParty xdmf2 uses sbrk which has been marked deprecated but
    # produces a warning without ThirdParty ala
    # /usr/include/unistd.h:587:7: note: 'sbrk' has been explicitly marked deprecated here
    "note: 'sbrk' has been explicitly marked deprecated here"

    # libproj source code includes the line
    # return 0;       /* suppresses a warning */
    # which is listed as context for another warning
    # and causes a match due to the work warning in it
    "return 0;       /\\* suppresses a warning \\*/"

    # boost graph lib causes a warning on gcc with code that
    # boost uses internally. An example for you template fans is
    # /source/Infovis/BoostGraphAlgorithms/Testing/Cxx/TestBoostAdapter.cxx:221:47: warning: '*((void*)(& ei)+32).__gnu_cxx::__normal_iterator<boost::detail::stored_edge_property<long unsigned int, boost::property<boost::edge_index_t, unsigned int> >*, std::vector<boost::detail::stored_edge_property<long unsigned int, boost::property<boost::edge_index_t, unsigned int> >, std::allocator<boost::detail::stored_edge_property<long unsigned int, boost::property<boost::edge_index_t, unsigned int> > > > >::_M_current' may be used uninitialized in this function [-Wmaybe-uninitialized]
    "[Bb]oost[Gg]raph.*edge_property.*may be used uninitialized"

    # Boost.Graph uses deprecated Boost headers internally.
    "define BOOST_BIND_GLOBAL_PLACEHOLDERS to retain the current behavior"

    # Boost.Math is C++14; ignore warnings for now.
    "boost/math/tools/config.hpp.*: warning: .*minimum language standard"

    # Warnings due to DIY
    "xmemory.*: warning C4996:.*Default Bounds constructor"

    # viskores uses deprecated TBB headers
    "TBB Warning: tbb/task.h is deprecated"

    # viskores related warnings, someone working on viskores should fix these and once fixed
    # remove these suppressions
    "struct VISKORES_DEPRECATED"
    "viskores::filter"

    # eigen uses this internal header from CUDA that should not be used.
    # [-Wcpp]
    "host_defines.h is an internal header file and must not be used directly.  This file will be removed in a future CUDA release.  Please use cuda_runtime_api.h or cuda_runtime.h instead"

    # warnings from moc generated code such as
    # GUISupport/Qt/GUISupportQt_autogen/EWIEGA46WW/moc_QVTKInteractorInternal.cpp:73:55: warning: redundant parentheses surrounding declarator [-Wredundant-parens]
    "autogen.*moc.*redundant parentheses"
    # GUISupport/Qt/GUISupportQt_autogen/EWIEGA46WW/moc_QFilterTreeProxyModel.cpp:40:5: warning: 'StringRefStorage' may not intend to support class template argument deduction [-Wctad-maybe-unsupported]
    "autogen.*moc.*ctad-maybe-unsupported"

    # third party hdf5
    # if(NULL == (*full_name = (char *)H5MM_malloc(path1_len + path2_len + 2 + 2))) /* Extra "+2" to quiet GCC warning - 2019/07/05, QAK */
    "H5MM.*quiet GCC warning"

    # Ignore diy2 warnings
    "vtkdiy2"

    # missing overrides may be suppressed, but the superclass shows up as a match on some systems
    # it makes no sense to suppress the offending class but report the superclass
    # vtkPolyDataAlgorithm.h:91:3: note: overridden virtual function is here
    "note: overridden virtual function is here"

    # Warnings from template instantiations. Remove once cmake/cmake!4766 is releasted.
    "x(memory0|utility)\\([0-9]+\\): warning C4244"

    # Timeout errors of SCCACHE are common cases for long builds such as VTK+Viskores+CUDA
    "sccache: warning: The server looks like it shut down unexpectedly, compiling locally instead"

    # Ignore warnings from Apple's new linker about duplicate rpath flags.
    "ld: warning: duplicate -rpath"

    # Libxml2 has an ignored warning in a line with a string containing `warning`
    "xmlVFormatLegacyError.*validity warning"

    # Suppress deprecated CUB Swap function warning suggesting cuda::std::swap
    "cub::CUB_200802_SM_600::Swap.*deprecated.*cuda::std::swap"
  )
endif ()

# CI-specific warning suppressions.
#
# Some of our CI ends up generating warnings that don't really matter. Of
# particular interest are warnings which have different behavior in older
# compilers than modern ones. It's not really all that important to cater to
# old, broken warning implementations when newer compilers tell us when we
# aren't doing things properly.
if (NOT "$ENV{CI}" STREQUAL "")
  # Warning exclusions for old compilers and dependencies.
  if ("$ENV{CMAKE_CONFIGURATION}" MATCHES "mindeps")
    # GCC 8.0.0
    if ("$ENV{CMAKE_CONFIGURATION}" MATCHES "el7")
      list(APPEND CTEST_CUSTOM_WARNING_EXCEPTION
        # Deprecated classes trigger "usage" warnings in methods marked with
        # either `= default` or `= delete` generated implementations. This is not
        # helpful, so just ignore them all.
        "Wdeprecated-declarations"
      )
    endif ()
  endif ()

  # For some reason, warning flags aren't working here.
  if ("$ENV{CMAKE_CONFIGURATION}" MATCHES "_ext_vtk")
    list(APPEND CTEST_CUSTOM_WARNING_EXCEPTION
      "Wstringop-overflow="
      "note: destination object allocated here"
      "note: destination object '.*' of size .*"

      "Wstringop-overread"
      "allocated by 'operator new'"
      "note: source object '.*' of size .*"
      "note: at offset .* into source object '.*' of size .*"
      )
  endif ()
  if ("$ENV{CMAKE_CONFIGURATION}" MATCHES "wasm")
    list(APPEND CTEST_CUSTOM_WARNING_EXCEPTION
      # Emscripten tends to use bleeding edge versions of clang.
      # Suppress warnings from clang's c++ headers about deprecated features.
      "note: '[a-z]?strstream' has been explicitly marked deprecated here"
      "note: 'char_traits<vtkfmt::detail::char8_type>' has been explicitly marked deprecated here"
      "warning: 'char_traits<vtkfmt::detail::char8_type>' is deprecated"
    )
  endif ()
endif ()

list(APPEND CTEST_CUSTOM_COVERAGE_EXCLUDE
  "vtk[^\\.]+(Java|Python).cxx"
  ".*Testing.Cxx.*cxx"
  ".*Testing.Cxx.*h"
  ".*moc_.*cxx"

  # Exclude files from the Utilities directories
  ".*/Utilities/.*"
  ".*/ThirdParty/.*")
