Commit Graph

296 Commits

Author SHA1 Message Date
Bolshakov f9f4fd2eef Test reporting types hidden in tpl arguments
The full type use reporting in the three covered cases is ensured
with excessive `CanIgnoreType` calls which check not only the type
to be reported but also the type before dereferencing (which occurs
to be in the resugaring map). This approach should be refactored
in subsequent commits.
2023-02-01 21:26:28 +01:00
Bolshakov eb1697e002 Fix comment 2023-02-01 21:26:28 +01:00
Kim Gräsman 81ee985377 Ignore uses of declarations from inside of functions
Using type deduction, a function can define a local type and return it:

    auto func() {
      struct X {};
      return X();
    }

Before this change, IWYU would register X as a use for callers of func.

In certain situations (union inside lambda inside macro) this would lead
to a forward-declare use trying to format a forward-declaration of the
type local to the function in PrintForwardDeclare. This led to a crash
since the lambda was unnamed, but there's really no point trying to
forward-declare a type that is private to a function.

Generalize this so we ignore all uses of symbols declared inside a
function. In order to get in contact with that symbol, we must already
have access to the function, so the containing header must already be
included.

Fixes issue: 795
2023-01-22 20:35:42 +01:00
Kim Gräsman bd305afe7d Support 'IWYU pragma: export' for forward declarations
Covers both begin_export/end_export blocks and single-line export
pragmas.

Like with 'IWYU pragma: keep' marks the forward decl as automatically
desired to avoid removing manually exported but unused decls.

Add a simple testcase and update documentation.
2023-01-22 20:32:32 +01:00
Bolshakov 971a300bb9 Report only explicit typedefs
Previously, ReportDeclUse was called for TypedefDecl when TypedefType
is passed to ReportTypeUse. This change short-circuits that path. But
an underlying type should still be reported if the typedef doesn't
"provide" it. Hence, corresponding logic is moved into ReportTypeUse.
So ReportDeclUse reports typedef declarations and ReportTypeUse reports
typedef underlying types now.
2023-01-22 20:08:02 +01:00
Bolshakov 8a6009f587 Test using-declaration referring to typedef
Test case added in order not to break it at subsequent tweaking.
2023-01-22 20:08:02 +01:00
Bolshakov a5bcb853b9 Format test case properly 2023-01-22 20:08:02 +01:00
Andrey Ali Khan Bolshakov d3e23c367b
Fwd decls for explicit types only
Report fwd-decls only for explicitly written types

There is no point in reporting forward-declaration of a type when just
its pointer is used and the type is not explicitly written
in the source. Explicitly written pointer types are handled, e.g.,
in VisitTemplateSpecializationType and VisitTagType.

Add test cases for redundant fwd-decl absence
2023-01-07 18:06:15 +01:00
Kim Gräsman 849c2a2f55 Format all lines with trailing whitespace
This is the result of:

    git grep -n "\s$" | grep-format

No functional change.
2023-01-07 12:27:16 +01:00
Kim Gräsman cced95dba5 Do not crash if IsInHeader is called for builtin
In very particular circumstances (see test case), VisitFunctionDecl
would see a function definition that was an implicit constructor of a
builtin (va_list_tag).

Rather than crashing for implicit code, just say it's not in a header.

This feels a little questionable, because depending on how IsInHeader is
used, it might be misleading that it returns false for builtins/implicit
code. But I think it makes sense for now.

Fixes issue #1162.
2022-12-30 12:45:24 +01:00
Bolshakov c3bcb661f6 Handle sugared template specs in template args
Non-sugared template specialization type template arguments are already
analyzed due to 'TraverseType' call inside
'InstantiatedTemplateVisitor::TraverseSubstTemplateTypeParmTypeHelper'
and subsequent 'TraverseTemplateSpecializationTypeHelper' call.
Components of sugared template specialization types are added into
resugar_map, otherwise their template arguments would not be reported.
Test case has been extended.
2022-12-16 22:22:31 +01:00
Bolshakov 06f7c21cbc Test template instantiation reporting 2022-12-16 22:22:31 +01:00
Bolshakov 526827957b Fix template instantiation reporting
Full template specialization type use requires full information about
its template-argument-dependent fields and nested typedefs. But earlier,
it wasn't reported in many cases when template specialization type isn't
written explicitly in non-fwd-decl context.
2022-12-16 22:22:31 +01:00
Bolshakov ac93b84f01 Change member variable name in test
Names starting with an underscore aren't allowed in a global namespace,
according to the standard.
2022-12-16 22:22:31 +01:00
Bolshakov 7b9c04c0d3 Apply formatting 2022-12-16 22:22:31 +01:00
Bolshakov 42fece0244 Remove superfluous semicolons after method bodies 2022-12-16 22:22:31 +01:00
Kim Gräsman 616927dec4 Avoid dynamic mapping to self for reverse macro dependency
Commit 04f1c92537 added a mechanism that
dynamically adds a mapping from the includee to the includer in this
case:

    // a.h
    #if CONTROL_FLAG
    void foo();
    #else
    void bar();
    #endif

    // b.h
    #define CONTROL_FLAG
    #include "a.h"

However, there was no check that includee and includer were different,
so regular #include guards would cause self-mappings, which would
eventually lead to cycles in mappings and assertion failures, e.g.:

    Cycle in include-mapping:
      <z.hpp> ->
      <z.hpp>
    ,,,/iwyu_include_picker.cc:845: Assertion failed: Cycle in include-mapping
    Aborted

Avoid this by checking if the file defining the macro is the same as the
file using it; in that case we don't need a mapping.
2022-12-04 16:18:13 +01:00
Kim Gräsman 68646a5efd Work around dependent template alias crash
Only attempt to report decl uses of template names that have an
underlying template decl. This ignores uses of dependent templates and
potentially other such variations.

Even if that causes us to miss reporting for some valid constructs, it
allows further analysis of files that would otherwise crash IWYU.

Fixes issue #1140
2022-12-04 16:09:05 +01:00
Bolshakov b20224b9fc Fix opaque decl of enum with bool underlying type
Boolean type is internally represented in clang as '_Bool' builtin type.
In order to display it correctly taking into account language options,
actual printing policy should be used.
2022-11-25 20:24:25 +01:00
Daniel Hannon cf1624a4e2 Add begin_keep and end_keep pragmas, test and docs
this was a proposed issue in #1095 where one could
make a block of keeps as opposed to marking every
keep with a pragma: keep instruction.

added test to verify and updated documentation
where appropriate.
2022-11-02 19:33:10 +01:00
Jean-Philippe Gravel 6fb66575fb Add support for group/backreferences regex replacement in mapping files.
This is useful to generically remap files from one directory to another.
The following mapping would for instance map all files under the "foo/"
directory to "third_party/foo/", for instance, mapping "foo/bar.h" to
"third_party/foo/bar.h".

[ { include: ['@"(foo/.*)"', private, '"third_party/\1"', public ] } ]

This is useful for large project where different third_party libraries
are compiled together. In Chromium for instance, include paths would
look like:
 -Ithird_party -Ithird_party/v8/include

This allows code in V8 to include its own headers directly. Other
third_parties would include them via "third_party/v8/include/...".
Because files are accessible from two different paths, IWYU can't know
which should be used. Using a mapping file, adding or removing the
"third_party/v8/include" prefix where needed, resolves this problem.
2022-10-09 20:40:18 +02:00
jspam b74819dc5f
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>
2022-10-09 16:35:19 +02:00
Bolshakov aa9a2d2a55 Test "autocast" rules on header-defined functions 2022-10-08 16:17:50 +02:00
Bolshakov bbbae2d858 Clarify comment 2022-10-08 16:17:50 +02:00
Bolshakov 862812049a Avoid 'autocast' reporting for function definition
A function may just transmit passed-by-reference parameter somewhere.
Requirement to explicitly write forward declaration in the same file
(.cpp-file) to avoid '#include' suggestion is impractical when that type
is already fwd-declared in the corresponding header.
'Autocast' may still make sense for header-defined functions, due to
unlimited number of possible callers, so analysis of those fuctions
is kept.

Both function declaration site handling and call site handling
are changed.
2022-10-08 16:17:50 +02:00
Kim Gräsman c1d8dd310a Explicitly ignore std::find result in iterator test
Some standard libraries have a [[nodiscard]] attribute on std::find,
which leads to a warning about unused return value, which in turns masks
any real results from the test.

Avoid std::ignore since this test is older than C++11.
2022-10-03 16:45:37 +02:00
Andrey Ali Khan Bolshakov 96f940149d
Test 'autocast' call site handling more precisely
Check that only the last parameters require full type info for autocast.
2022-10-03 16:44:56 +02:00
Bolshakov ff1b866acf Test 'autocast' to value and reference separately
It is needed in order to be able to add definition to passing-by-ref
function and test on it "autocast" rules, not type info requirement
due to parameter variable definition.
New 'DirectStruct*'s and 'TplDirectStruct*'s are added because
requirement to '#include' their header is tested only by that
'#include', no inline diagnostics are emitted.
2022-10-03 14:53:56 +02:00
Bolshakov 2b6cc114d0 Reformat autocast test files
This prepares for adding some new well-formatted code.

No functional change.
2022-10-03 14:53:56 +02:00
Bolshakov 1d053edce2 Move autocast testing from badinc
More cases are covered by iwyu_stricter_than_cpp.
2022-09-12 18:58:19 +02:00
Kim Gräsman 164b8fe759 [clang compat] Add explicit -std=gnu++98 to badinc.cc
Clang recently moved to gnu++17 by default, where dynamic exception
specifications are no longer supported. Upstream change:
3e99b8d947

Since badinc.cc is all pre-C++11 code, pin the language standard for
it to gnu++98, which has a number of interesting benefits:

* makes it less rewarding to add new tests here
* any standard library changes have significantly lower impact
* it's about 30% faster

No functional change.
2022-09-11 21:53:28 +02:00
Bolshakov 3db4d4ae02 Test absence of redundant enum reporting 2022-09-04 13:20:54 +02:00
Bolshakov 8751dac97b Avoid redundant enum type reporting
There is no need in reporting enum opaque declaration or full use if
enumeration type name or enumerator name isn't explicitly written,
because it should already be present elsewhere in the translation unit,
probably through included headers.
2022-09-04 13:20:54 +02:00
Kim Gräsman 5b5dd13746 Add new testcase for --regex=ecmascript
The default for --regex is llvm, so it's covered by any existing tests
with mappings.
2022-09-03 15:04:27 +02:00
Kim Gräsman eee6d6a199 Drop -save-temps command-line args
IWYU doesn't produce any outputs, so -save-temps does not have any useful
effect.

Before this patch, passing -save-temps did, however, generate multiple
compilation jobs which caused a fatal error:

  error: unable to handle compilation, expected exactly one compiler job

Filter the arguments out, similar to what Clang tools do by default.

Fixes issue #1060.
2022-09-01 20:03:59 +02:00
Kim Gräsman 77e9128cba Replace all uses of RemoveElaboration with Desugar
Desugar performs the same desugaring as RemoveElaboration, and more.

This patch is a straight:

  git grep -l "RemoveElaboration" | xargs sed -i -e 's/RemoveElaboration/Desugar/'

followed by a test cleanup in badinc.cc, where Desugar sees through more sugar
than RemoveElaboration, and actually produces better results.
2022-08-31 22:16:16 +02:00
Kim Gräsman d2d4d15d8d Allow forward-declaration of elaborated types in type decls
This covers the common C pattern of declaring a typedef for a struct before the
struct itself, e.g.

   typedef struct foo foo_t;

   struct foo {
     int value;
     foo_t *next;
   };

Fixes issue #1065.
2022-07-24 22:02:19 +02:00
Bolshakov 0f7a7aba8c Report aliased template parameter
Template-nested typedef can't be responsible for any parameter
of the template, because an exact type is known only on template
specialization.
2022-07-23 13:18:27 +02:00
Bolshakov e0ae900e84 Improve template typedef test
Previously, it doesn't really check the need of including a file
with a type referred to by typedef from specialized template
'Container<Class>', because that file was reported due to declaration
of 'Pair' typedef. Now, it shows an error: Class1 full usage through
nested typedef or alias doesn't trigger full type requirement.
2022-07-23 13:18:27 +02:00
Bolshakov 57b812bf35 Report C++20 concept declaration use 2022-07-23 10:31:48 +02:00
Bolshakov b46a96e5a6 Fix autocast to reference
CastExpr for constructor conversion is absent in the AST in such a case
2022-07-16 18:30:23 +02:00
Kim Gräsman bab51b55ab Mark std::less<> specializations in tests const
Some standard libraries require that any std::less<> specializations have a
const operator(). That seems hygienic anyway, so add a const qualifier.

Fixes the precomputed_tpl_args test on FreeBSD (and probably Mac).
2022-07-16 15:47:43 +02:00
Bolshakov 4c0f396159 Tests on enum opaque declarations 2022-06-13 07:47:21 +02:00
Bolshakov 12d2c18fa6 Report enum type name instead of constant name
The reason is that
1) enumerators led to much noise in explanatory comments
(the idea is similar to cedf9d6984323a0a68847d), and
2) without this change, EnumClass::Item is reported separately
as fwd-decl use of EnumClass along with the full use
of EnumClass::Item.

Unnamed enums are an exception.
2022-06-13 07:47:21 +02:00
Bolshakov fb41044b9f Suggest enumeration opaque declarations
Opaque (i.e., in fact, forward) declarations are allowed for scoped
enumerations and unscoped ones with underlying type explicitly
specified.
2022-06-13 07:47:21 +02:00
Bolshakov 799a8ef1b5 Replace RecordDecl by TagDecl
This is preparation for adding suggestions for enumeration opaque
(forward) declarations. Enums should be treated almost similar
to classes and structs, so clang::RecordDecl should be replaced
by clang::TagDecl, which is a superclass for RecordDecl and EnumDecl,
in many places.
2022-06-13 07:47:21 +02:00
Bolshakov 8e6c16dbb5 Maintenance: formatting 2022-05-28 22:54:19 +02:00
Bolshakov db69a0c4d3 Perform full analysis of typedef components
We used to ignore all analysis of AST nodes inside a `typedef`, but that
caused us to miss author-intent analysis of nested typedefs.

Remove the special casing for member-of-typedef now that reporting as a
whole is less granular (we now report only the parent type instead of
all nested components).

Add test cases to demonstrate that nested typedefs observe
the author-intent rules.
2022-05-28 22:54:19 +02:00
Bolshakov 496d200ce0 Avoid double reporting of all class members
In addition to C++ methods, silence reports of all declarations inside a
class (data members, types, etc.) if the parent type has already been
reported.
2022-05-28 22:54:19 +02:00
Daniel Hannon 3f456f66c2 add --comment_style option with tests 2022-05-28 11:03:47 +02:00