wall found compilation errors
play

-Wall found compilation errors Aditya Kumar Facebook Enable - PowerPoint PPT Presentation

-Wall found compilation errors Aditya Kumar Facebook Enable compiler warnings with -Wall -Werror Compiler warnings are precise static analysis tools -Wall enables most relevant compiler warnings Widely used warnings, well


  1. -Wall found compilation errors Aditya Kumar Facebook

  2. Enable compiler warnings with -Wall -Werror ● Compiler warnings are precise static analysis tools ● -Wall enables most relevant compiler warnings ○ Widely used warnings, well tested, less chance of false positives ● Adding -Werror fails the compilation

  3. -Wpointer-bool-conversion bool foo(); void bar(bool x) { if (foo && x) { // ... } } error: address of function 'foo' will always evaluate to 'true' [-Werror,-Wpointer-bool-conversion]

  4. -Wself-assign class foo { public: foo(int a) { _a = _a; } private: int _a; }; error: assigning field to itself [-Werror,-Wself-assign] PS: Using IDE can help because of syntax highlighting.

  5. -Winfinite-recursion // test.cpp: // library1.h: #include<library1.h> #define _aligned_free free #include<folly/Memory.h> int main() // folly/Memory.h { void _aligned_free(void *p) { free(p); int *p2 = _aligned_alloc(64, 10*sizeof *p2); } // ... _aligned_free(p2); } error: all paths through this function will call itself [-Werror,-Winfinite-recursion]

  6. -Wunused-labels void checkSeverity(SeverityType T) { #include<string> switch(T) { using namespace std; Normal: return; // Do nothing void foo() { High: warn(); return; std: Critical: cleanup(); return; string a("asdfasdf"); default: return; } } } Typing too fast? Case against case statement? error: unused label 'std' [-Werror,-Wunused-label]

  7. -Woverloaded-virtual // Base.h #include<Base.h> class Base // Derived.h { class Derived : Base { public: virtual void foo(unsigned a); public: }; void foo(int a); }; error: 'Derived::foo' hides overloaded virtual function [-Werror,-Woverloaded-virtual] note: hidden overloaded virtual function 'Base::foo' declared here: type mismatch at 1st parameter ('unsigned int' vs 'int')

  8. How to enable -Wall for a large codebase ● Directory level enabling is tedious ● Build applications with -Wall -Werror and continue the build to collect as many errors (e.g., -k flag in make) ● With some bash magic get the list of all the warnings ○ e.g., grep 'error:' build.log | sed 's/.*Werror,//g' | sed 's/\]//g' | sort -u ● Disable those warnings (by adding their -Wno versions) ● Keep fixing one warning at a time… ● Disable warnings in the build target where changing the code isn’t possible ○ Use pragma (e.g., #pragma clang diagnostic ignored "-Woverloaded-virtual") if necessary. ● Some warnings appear way more than others ○ -Wreorder ○ -Wpessimizing-move ○ -Woverloaded-virtual

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend