0

I want to use clang-tidy to find out all uncaught exceptions by its direct or indirect caller, up to main, but this demo has no effect. Generally, I want to clang-tidy to report that the function cc is throwing an exception that not being caught by main, but it reports nothing, I want to know why & how can I achieve my goal? Thks

main.cpp

#include <iostream>
int m = 0;
extern int cc() throw();

int ff() {
  if (m > 0) {
    throw "H";
  }
  return 1;
}

int gg() {
  if (m > 0) {
    return ff();
  } else {
    return 1;
  }
}


int main() {
  return cc();
}

cc.cpp

extern int m;
int cc() {
  if (m > 0) {
    return 1;
  }
  throw "D";
}

.clang-tidy

Checks: '-*,bugprone-exception-escape' 
CheckOptions: 
    - key: FunctionsThatShouldNotThrow
      value: main
HeaderFilterRegex: '.*' 

command

clang-tidy main.cpp cc.cpp
ddwolf
  • 91
  • 2
  • 10
  • `int cc() throw();` (or `noexcept` in more recent version), so exception of `cc` doesn"t escape to `main`. As `throw "D";` is uncaught, it would call [`std::unexpected()`](https://en.cppreference.com/w/cpp/error/unexpected). – Jarod42 Nov 18 '21 at 11:47

1 Answers1

0
  1. it turns out that clang-tidy can not detect cross file checks. If I concatenate the two cpp files, clang-tidy can identify the uncaught exception successfully.
  2. the configuration file for clang-tidy is wrong, the key in CheckOptions should be bugprone-exception-escape.FunctionsThatShouldNotThrow instead of FunctionsThatShouldNotThrow
ddwolf
  • 91
  • 2
  • 10