Use more exact location for caught exceptions

This change provides better location info when using macros such as:

    BOOST_CHECK_THROW(..., Exc);

The use of type Exc would be attributed to the file defining the macro
before this patch, but is now correctly attributed to the expansion
location.

Co-authored-by: Kim Gräsman <kim.grasman@gmail.com>
This commit is contained in:
jspam 2022-10-09 16:35:19 +02:00 committed by GitHub
parent aa9a2d2a55
commit b74819dc5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 87 additions and 3 deletions

11
iwyu.cc
View File

@ -1871,10 +1871,15 @@ class IwyuBaseAstVisitor : public BaseAstVisitor<Derived> {
bool VisitCXXCatchStmt(clang::CXXCatchStmt* stmt) {
if (CanIgnoreCurrentASTNode()) return true;
if (const Type* caught_type = stmt->getCaughtType().getTypePtrOrNull()) {
// Strip off pointers/references to get to the 'base' type.
if (const VarDecl* exception_decl = stmt->getExceptionDecl()) {
// Get the caught type from the decl via associated type source info to
// get more precise location info for the type use.
TypeLoc typeloc = exception_decl->getTypeSourceInfo()->getTypeLoc();
const Type* caught_type = typeloc.getType().getTypePtr();
// Strip off pointers/references to get to the pointee type.
caught_type = RemovePointersAndReferencesAsWritten(caught_type);
ReportTypeUse(CurrentLoc(), caught_type);
ReportTypeUse(GetLocation(&typeloc), caught_type);
} else {
// catch(...): no type to act on here.
}

View File

@ -0,0 +1,33 @@
//===--- catch-exceptions-macro.h - test input file for iwyu --------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef INCLUDE_WHAT_YOU_USE_TESTS_CXX_CATCH_EXCEPTIONS_MACRO_H_
#define INCLUDE_WHAT_YOU_USE_TESTS_CXX_CATCH_EXCEPTIONS_MACRO_H_
// Named catch
#define CHECK_THROW_1(E) \
do { \
try { \
; \
} catch (const E& ex) { \
(void)ex; \
} \
} while (0)
// Unnamed catch
#define CHECK_THROW_2(E) \
do { \
try { \
; \
} catch (const E&) { \
; \
} \
} while (0)
#endif // INCLUDE_WHAT_YOU_USE_TESTS_CXX_CATCH_EXCEPTIONS_MACRO_H_

View File

@ -18,6 +18,8 @@
#include "tests/cxx/catch-byvalue.h" // for CatchByValye
#include "tests/cxx/catch-elab.h" // for CatchElab
#include "tests/cxx/catch-logex.h" // for LogException
#include "tests/cxx/catch-macro-1.h" // for CatchMacro1
#include "tests/cxx/catch-macro-2.h" // for CatchMacro2
#include "tests/cxx/catch-thrown.h" // for Thrown
#include <stdio.h>

15
tests/cxx/catch-macro-1.h Normal file
View File

@ -0,0 +1,15 @@
//===--- catch-macro-1.h - test input file for iwyu -----------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef INCLUDE_WHAT_YOU_USE_TESTS_CXX_CATCH_MACRO_1_H_
#define INCLUDE_WHAT_YOU_USE_TESTS_CXX_CATCH_MACRO_1_H_
class CatchMacro1 {};
#endif // INCLUDE_WHAT_YOU_USE_TESTS_CXX_CATCH_MACRO_1_H_

15
tests/cxx/catch-macro-2.h Normal file
View File

@ -0,0 +1,15 @@
//===--- catch-macro-2.h - test input file for iwyu -----------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef INCLUDE_WHAT_YOU_USE_TESTS_CXX_CATCH_MACRO_2_H_
#define INCLUDE_WHAT_YOU_USE_TESTS_CXX_CATCH_MACRO_2_H_
class CatchMacro2 {};
#endif // INCLUDE_WHAT_YOU_USE_TESTS_CXX_CATCH_MACRO_2_H_

View File

@ -10,6 +10,7 @@
// IWYU_ARGS: -fcxx-exceptions -fexceptions -I .
#include "tests/cxx/catch-exceptions.h"
#include "tests/cxx/catch-exceptions-macro.h"
int main() {
try {
@ -51,6 +52,14 @@ int main() {
puts("Unknown exception");
}
// IWYU: CatchMacro1 needs a declaration...*
// IWYU: CatchMacro1...*catch-macro-1.h
CHECK_THROW_1(CatchMacro1);
// IWYU: CatchMacro2 needs a declaration...*
// IWYU: CatchMacro2...*catch-macro-2.h
CHECK_THROW_2(CatchMacro2);
return 0;
}
@ -63,6 +72,8 @@ tests/cxx/catch.cc should add these lines:
#include "tests/cxx/catch-byvalue.h"
#include "tests/cxx/catch-elab.h"
#include "tests/cxx/catch-logex.h"
#include "tests/cxx/catch-macro-1.h"
#include "tests/cxx/catch-macro-2.h"
#include "tests/cxx/catch-thrown.h"
tests/cxx/catch.cc should remove these lines:
@ -74,7 +85,10 @@ The full include-list for tests/cxx/catch.cc:
#include "tests/cxx/catch-byref.h" // for CatchByRef
#include "tests/cxx/catch-byvalue.h" // for CatchByValue
#include "tests/cxx/catch-elab.h" // for CatchElab
#include "tests/cxx/catch-exceptions-macro.h" // for CHECK_THROW_1, CHECK_THROW_2
#include "tests/cxx/catch-logex.h" // for LogException
#include "tests/cxx/catch-macro-1.h" // for CatchMacro1
#include "tests/cxx/catch-macro-2.h" // for CatchMacro2
#include "tests/cxx/catch-thrown.h" // for Thrown
***** IWYU_SUMMARY */