Commit Graph

1277 Commits

Author SHA1 Message Date
Kim Gräsman 204ebafb22 [clang compat] Use getAs<> instead of dynamic cast for Types
Downcasting an AST Type* to a more specific type is increasingly unsafe in the
AST. In general any Type* picked out of the AST might contain so-called 'sugar',
i.e. visible or invisible structure that does not change the identity of the
underlying type, but adds information.

After Clang 15f3cd6bfc670ba6106184a903eb04be059e5977, almost every Type is
wrapped in an ElaboratedType with kind ETK_None (i.e. "no elaboration").

ElaboratedType counts as sugar, and thus needs to be stripped off before we do
dynamic type checking of Type nodes.

The recommended way to look through sugar downwards in the tree is
'type->getAs<MostDerivedType>()', which will recursively remove sugar until a
node of MostDerivedType is reached. If there is no MostDerivedType node it
returns nullptr, so it's safe to consider getAs a desugaring dynamic downcast.

There are still some places where we use DynCastFrom or isa<> on Types, I have
only modified the ones that had any effect on broken testcases. Planning to look
into the rest separately once the test suite is working.

This fixes 4 broken testcases, and is an improvement towards fixing #1092.
2022-08-28 16:55:43 +02:00
Kim Gräsman 78a4fe951a [clang compat] Use elaborated type specifier in CanForwardDeclareType
Use the new IsElaboratedTypeSpecifier instead of node type-checking.

Just because an AST node is an ElaboratedType does not mean it's an
actual elaborated type specifier (which is what we care about here).

This fixes 10 broken testcases after Clang 15f3cd6bfc670ba6106184a903eb
wrapped every Type inside an ElaboratedType in the AST, but it is
strictly a bugfix in general: all elaborated type nodes are not type
specifiers.

Part of fix for #1092.
2022-08-28 16:16:04 +02:00
Kim Gräsman 0be9166fe6 Rename IsElaborationNode to IsElaboratedTypeSpecifier
'Elaborated type specifier' is the formal term for 'struct X', 'class Y' or
'union Z'. Use it to avoid potential confusion around what 'elaboration node'
means.

Clean up a comment that claimed 'MostElaboratedAncestor' used
'IsElaborationNode', where it actually doesn't.

No functional change.
2022-08-28 16:16:04 +02:00
Kim Gräsman 941fc19785 Add 'printtypeclass' debug flag
Vanilla PrintableType just prints the type name, on a declaration-like form.
But it's sometimes useful to see also what AST node the type comes from
(e.g. PointerType, ElaboratedType, RecordType), particularly when
investigating changes in the Clang AST.

Add a debug flag 'printtypeclass' to change type printing from e.g.:

   struct IndirectClass

to:

   ElaboratedType:struct IndirectClass
   RecordType:struct IndirectClass

depending on the actual dynamic class of the AST type node.
2022-08-28 14:58:49 +02:00
Kim Gräsman 4217b5a20e Add --debug argument for disposable debug flags
Sometimes for debugging and testing, it helps to be able to tweak IWYU behavior
beyond the log verbosity.

Add a free-form --debug argument that takes a comma-separated list of flag
names. No validation is performed on these flags, they just serve as a global
switchboard to enable/disable custom behavior.

Note that these are not intended as a replacement for switches in general, just
as a support structure for quickly adding conditional debug behavior while
troubleshooting.
2022-08-28 14:58:49 +02:00
Kim Gräsman e94a78b3a5 [cmake] Require C++17 as does LLVM 2022-08-28 14:52:23 +02:00
Kim Gräsman 39e45aa453 [ci] Work around missing libclang-16.so.1 2022-08-28 14:52:23 +02:00
Kim Gräsman 8e65d9cf4a [ci] Remove llvm-15 packaging workarounds
Snapshot packages are now llvm-16.
2022-08-09 22:47:36 +02:00
Alejandro Colomar 983fe5a8fb Add another mapping for NULL
POSIX defines NULL in <unistd.h> too.

Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
2022-08-01 20:10:27 +02:00
Kim Gräsman bfe1909a9c Remove fruitless Base::Visit calls
The IwyuBaseAstVisitor has a base class, BaseAstVisitor, but it only has a
small number of Visit functions implemented, for logging.

So there is no need to call Base::VisitX in general, only for the few with
an implementation that actually does something, and they are preserved
here.
2022-07-24 22:15:58 +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
Kim Gräsman 7ca0a75b85 [clang compat] Harden handling of 'final' specifier
Clang 9f57b65a272817752aa00e2fb94154e6eed1d0ec sometimes prints the 'final'
keyword twice for a declaration (tracking bug:
https://github.com/llvm/llvm-project/issues/56517).

This triggered several bugs in MungedForwardDeclareLineForTemplates:

* Only removed first 'final'
* Removed one character too many (sizeof(char[]) includes the trailing NUL)

Instead, replace all occurrences of 'final' and do it first so the stripping of
superclasses and body also get rid of trailing spaces. This should now work even
if the upstream bug is fixed.
2022-07-16 15:43:31 +02:00
Alejandro Colomar 979002bf8e Add mappings for SIGABRT
Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
2022-07-09 21:59:46 +02:00
Alejandro Colomar fdca99fcd2 Add mappings for 'struct iovec'
As recently documented in iovec(3type).

Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
2022-07-09 21:59:46 +02:00
Alejandro Colomar ec625ea9db Add mappings for 'MAP_POPULATE'
It's not in POSIX, so I found it experimentally.

$ grepc -k MAP_POPULATE
./asm-generic/mman-common.h:26:#define MAP_POPULATE		0x008000	/* populate (prefault) pagetables */
./x86_64-linux-gnu/bits/mman-map-flags-generic.h:34:# define MAP_POPULATE	0x08000		/* Populate (prefault) pagetables.  */

$ grep -r '<bits/mman-map-flags-generic.h>'
x86_64-linux-gnu/bits/mman.h:#include <bits/mman-map-flags-generic.h>
x86_64-linux-gnu/bits/mman-map-flags-generic.h:# error "Never use <bits/mman-map-flags-generic.h> directly; include <sys/mman.h> instead."

So glibc defines it in <sys/mman.h>.

$ grep -r '<asm-generic/mman-common.h>'
asm-generic/mman.h:#include <asm-generic/mman-common.h>
$ grep -r '<asm-generic/mman.h>'
x86_64-linux-gnu/asm/mman.h:#include <asm-generic/mman.h>
$ grep -r '<asm/mman.h>'
linux/mman.h:#include <asm/mman.h>

And the kernel in <linux/mman.h>.

Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
2022-07-09 21:59:46 +02:00
Alejandro Colomar a7d6088546 Add mappings for 'MAXHOSTNAMELEN'
I couldn't find an documentation about where it should be defined
(POSIX doesn't define it), so my best guess was to find the
definitions experimentally.  Combining my findings in the glibc
repo with the findings in my own Debian system, and ignoring
kernel headers and unrelated headers such as X11's, this is my
best guess.

See:

alx@asus5775:/usr/include$ grepc -k MAXHOSTNAMELEN
./X11/Xtrans/Xtranssock.c:225:#define MAXHOSTNAMELEN 255
./asm-generic/param.h:17:#define MAXHOSTNAMELEN	64	/* max length of hostname */
./protocols/timed.h:44:#define MAXHOSTNAMELEN	64
./x86_64-linux-gnu/ruby-3.0.0/rb_mjit_min_header-3.0.4.h:24134:#define MAXHOSTNAMELEN 64
./x86_64-linux-gnu/sys/param.h:54:# define MAXHOSTNAMELEN	HOST_NAME_MAX

alx@asus5775:~/src/gnu/glibc$ grepc -kx '\.h$' MAXHOSTNAMELEN
./inet/protocols/timed.h:44:#define MAXHOSTNAMELEN	64
./misc/sys/param.h:54:# define MAXHOSTNAMELEN	HOST_NAME_MAX
./sunrpc/rpc/types.h:102:#define        MAXHOSTNAMELEN  64

Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
2022-07-09 21:59:46 +02:00
Alejandro Colomar 21d4c7d2df Add mappings for 'struct tm'
Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
2022-07-09 21:59:46 +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
Daniel Hannon 67942da411 clarify purpose of --update_comments in help screen and manpages 2022-05-28 11:03:40 +02:00
Kim Gräsman 1375e56503 Consider all null decls ignorable
Non-template code only allows the null decl pointer to be ignored.

For instantiated templates, CanIgnoreDecl is quite sophisticated and uses a
set to ignore subtrees we've visited before, but it doesn't explicitly ignore
the null decl.

There are cases in templated code where the decl can be null as well -- most
prominently for builtin types.

With this test input:

  $ cat t.cc
  #include <algorithm>
  #include <cassert>
  #include <vector>

  void f() {
    std::vector<int> v;
    assert(std::none_of(v.cbegin(), v.cend(),
                        [](auto value) { return value == 100; }));
  }

  $ include-what-you-use -stdlib=libc++ t.cc

(note the explicit use of libc++)

std::vector<int>::const_iterator collapses to a 'const int *', which has no
declaration, and so yields a null decl. Prior to this fix, that would lead to a
cast assertion or segfault attempting to cast the resulting null pointer to a
FunctionDecl in GetCanonicalUseLocation.

Add an assertion for non-null to GetCanonicalUseLocation to clarify that
null filtering must happen before canonicalizing the use-location.

I have not found a way to capture this in a reduced testcase.

Fixes #989.
2022-05-27 18:11:55 +02:00
Aaron Puchert 5f38c0dd36 Minor improvements to man page formatting
In [=N] only N is a non-terminal, the = is a terminal and [] is neither.
When referring to another man page, only the name should be bold, not
the number in parentheses.

For referring to the example we don't need special fonts. The order and
naming of sections in a man page is standardized and examples are always
under this heading towards the end.
2022-05-27 14:57:31 +02:00
Kim Gräsman cb4d34477a [fix_includes] Recognize namespace aliases
... as different from Allman-style namespace definitions.

Fixes #1022.
2022-05-24 06:17:30 +02:00
Kim Gräsman da6064016e [ci] Add workaround for missing libMLIRSupportIndentedOstream.a 2022-05-22 12:23:03 +02:00
Alejandro Colomar fb7f8557a1 Add mappings for byteorder(3) functions
Update incidental use of htons(3) to use a private name to avoid influence of
the default mappings on test output.

Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
2022-05-21 10:49:29 +02:00
Kim Gräsman 9dee4b779b Print space between pointer and decl
This avoids confusing log output like "0x00405028void x();".
2022-05-19 21:41:34 +02:00
Kim Gräsman 14551a7126 Allow printing of null AST entities
Before, this would segfault attempting to print e.g. null decls.

These printers are predominantly used for logging, and it seems like a shame for
debug logging to crash on a null pointer if the code around it does not.

Remove now-unnecessary null-checks in logging.
2022-05-19 21:41:34 +02:00
Kim Gräsman f8c76cf633 Remove special printing of invalid locations
Clang itself prints invalid locations as "<invalid loc>", which is better for
consistency with other tools, and easier to spot in logs as something out of the
ordinary.
2022-05-19 21:41:34 +02:00
Kim Gräsman 5191af9e6c Clarify that Print... functions are intended for debugger use
Group them together and apart from Printable... functions.
2022-05-19 21:41:34 +02:00
Kim Gräsman cd6ee274dd Replace Print... with stream chaining
Replace both PrintStmt and PrintASTNode with their Printable counterparts.

No functional change intended.
2022-05-19 21:41:34 +02:00
Kim Gräsman e4f018d7fe [ci] Add workaround for missing tblgen-lsp-server 2022-05-19 21:41:34 +02:00
Jan Kokemüller 8bc14b8f04 Do not warn on unnamed struct fields 2022-05-18 20:21:09 +02:00
Kim Gräsman 3a793604c1 [cmake] Support LLVM external project build
This makes it possible to build IWYU as part of LLVM using the
LLVM_EXTERNAL_PROJECTS feature of the LLVM build system.

Add separate instructions for this build mode to README.

Based on original patch from @bc-lee:
https://github.com/include-what-you-use/include-what-you-use/pull/994
2022-04-25 18:44:26 +02:00
Kim Gräsman 59509beca4 [cmake] Use "standalone" instead of out-of-tree/in-tree
This is more consistent with other projects that can build as part of the
llvm-project tree or standalone, e.g. flang, lldb, compiler-rt.
2022-04-25 18:44:26 +02:00
Kim Gräsman f81b0fe26e [doc] Separate compatibility notes from build
Edit content a little bit to get rid of reference to "build bot".
2022-04-24 17:01:53 +02:00
Kim Gräsman 0636ec3f3e [doc] Reduce emphasis
This sentence came across as a heading; make it less imposing.
2022-04-24 17:01:53 +02:00
Kim Gräsman e23a2f9807 [doc] Use sentence-case for headings
The use of title-case feels dated and is harder to get right consistently.

Rewrite all headings to plain sentence-case.
2022-04-24 17:01:53 +02:00
Kim Gräsman 9c15de6da6 [cmake] Synthesize clang-headers target if it's missing
... not depending on a potentially-incidental condition.
2022-04-24 16:01:09 +02:00
Kim Gräsman 226efb43ad [cmake] Move Git revision lookup towards the top of the file
This is the same kind of activity as looking for LLVM/Clang packages, and
synthesizing targets, so let's do it up-front.

Move the compile-option setting together with the others.
2022-04-24 16:01:09 +02:00