Differences between revisions 33 and 34
Revision 33 as of 2020-02-25 00:22:40
Size: 4787
Editor: SamatJain
Comment: TSA
Revision 34 as of 2020-04-03 00:24:10
Size: 5094
Editor: SamatJain
Comment:
Deletions are marked like this. Additions are marked like this.
Line 54: Line 54:
Documentation:

 * [[https://insights.sei.cmu.edu/sei_blog/2014/10/thread-safety-analysis-in-c-and-c.html|Thread Safety Analysis in C and C++]]: CMU blog article on TSA
 * [[https://reviews.llvm.org/D14731|Thread safety analysis support for libc++, annotating mutex, lock_guard, etc]]
Line 58: Line 63:
 * [[https://insights.sei.cmu.edu/sei_blog/2014/10/thread-safety-analysis-in-c-and-c.html|Thread Safety Analysis in C and C++]]: CMU blog article on TSA
=== Lifetime safety analysis ===

 * [[https://github.com/mgehre/llvm-project|mgehre/llvm-project]]: clang implementation
 * note: gsl = Guidelines Support Library

See also:

C++ style guides

`const` correctness

Formatting

ClangFormat: auto-formats C++ code

Static analysis tools

  • Clang-Tidy: C/C++ linting tool, à la Google's cpplint.

clang-tidy -checks='*' *.cpp -- -std=c++11

# Highest severity warnings only (i.e. memory leaks, etc)
cppcheck --enable=all *.cpp
# Everything except style-related linting
cppcheck --enable=warning,performance,portability,information,missingInclude \
         --std=c++11 --library=std.cfg --verbose --quiet \
         *.cpp

Overview articles:

Thread safety analysis (TSA)

Documentation:

Example projects:

Lifetime safety analysis

Stuff I never remember

Unnecessary copies w/ `auto`

Beware unncessary copies w/ auto; default semantics is to make a copy.

// Typically there's no reason to copy.
for (const auto &Val : Container) { observe(Val); }
for (auto &Val : Container) { Val.change(); }

// Remove the reference if you really want a new copy.
for (auto Val : Container) { Val.change(); saveSomewhere(Val); }

// Copy pointers, but make it clear that they're pointers.
for (const auto *Ptr : Container) { observe(*Ptr); }
for (auto *Ptr : Container) { Ptr->change(); }

Don't evaluate `end()` in loops

// BAD
BasicBlock *BB = ...
for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
  // ... use I ...

// GOOD (unless you are mutating BB; use the above loop if you are, and document you are doing so)
BasicBlock *BB = ...
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
  // ... use I ...

Use `const` iterators if not mutating a structure

Expanding on the above:

BasicBlock *BB = ...
for (BasicBlock::const_iterator I = BB->cbegin(), E = BB->cend(); I != E; ++I)
  // ... use I ...

Dynamic loading of DLL/DSOs

Example of loading OpenCL dynamically. Uses a Python parser script to create special header file with defines that map both functions and does appropriate type conversion. https://github.com/opencv/opencv/pull/1542/files

Boost.DLL: Boost library for easily loading DLLs (e.g. for plugins). Cross platform, but requires runtime libraryes boost_system (integrated into C++11?) and boost_filesystem (integrated into C++17).

https://github.com/knusbaum/CPP-Dynamic-Class-Loading: Class for dynamically loading classes (Linux only)

SamatsWiki: CodingStyle/C++ (last edited 2020-09-02 20:49:51 by SamatJain)