Commit Graph

618 Commits

Author SHA1 Message Date
Kadir Cetinkaya 1a02c963e3
Revert "Revert "[clang] Dont print implicit forrange initializer""
This reverts commit 7aac15d5df.

Only updates the tests, as these statements are still part of the CFG
and its just the pretty printer policy that changes. Hopefully this
shouldn't affect any analysis.
2022-06-17 16:51:16 +02:00
Nico Weber 7aac15d5df Revert "[clang] Dont print implicit forrange initializer"
This reverts commit 32805e60c9.
Broke check-clang, see comments on https://reviews.llvm.org/D127863
2022-06-17 08:59:17 -04:00
Kadir Cetinkaya 32805e60c9
[clang] Dont print implicit forrange initializer
Fixes https://github.com/clangd/clangd/issues/1158

Differential Revision: https://reviews.llvm.org/D127863
2022-06-17 11:29:44 +02:00
Simon Pilgrim 43e7ba6495 Fix signed/unsigned comparison warning 2022-06-15 11:53:08 +01:00
Martin Boehme 1784fe7be7 [Clang] Fix signed-unsigned comparison warning that breaks the ppc64 build. 2022-06-15 10:53:14 +02:00
Martin Boehme 665da187cc [Clang] Add the `annotate_type` attribute
This is an analog to the `annotate` attribute but for types. The intent is to allow adding arbitrary annotations to types for use in static analysis tools.

For details, see this RFC:

https://discourse.llvm.org/t/rfc-new-attribute-annotate-type-iteration-2/61378

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D111548
2022-06-15 09:47:28 +02:00
isuckatcs caf2767c47 [Clang][AST] Fixed BindingDecl AST-dump for tuple like structures
The AST of a BindingDecl in case of tuple like structures wasn't
properly printed. For these bidnings there is information stored
in BindingDecl::getHoldingVar(), and this information was't
printed in the AST-dump.

Differential Revision: https://reviews.llvm.org/D126131
2022-06-14 21:13:04 +02:00
phyBrackets 9d637956b7 [clang][NFC][AST] rename the ImportError to ASTImportError
this patch is the continuation of my previous patch regarding the ImportError in ASTImportError.h

Reviewed By: martong

Differential Revision: https://reviews.llvm.org/D125340
2022-06-15 00:40:32 +05:30
Balázs Kéri f93dee1033 [clang][ASTImporter] Fix import of function with auto return type.
Fix a case of importing a function with auto return type
that is resolved with a type template argument that is declared
inside the function.
Fixes #55500

Reviewed By: martong

Differential Revision: https://reviews.llvm.org/D127396
2022-06-10 10:18:53 +02:00
Balázs Kéri d597a461e0 [clang][ASTImporter] Add support for import of UsingPackDecl.
Reviewed By: martong

Differential Revision: https://reviews.llvm.org/D125986
2022-06-01 09:58:08 +02:00
Vassil Vassilev 8c0eb32d2a Also remove the empty StoredDeclsList entry from the lookup table
In case where we have removed all declarations for a given declaration name
entry we should remove the whole StoredDeclsMap entry.

This patch improves consistency in the lookup tables and helps cling/clang-repl
error recovery.

Differential revision: https://reviews.llvm.org/D119675
2022-05-27 12:36:37 +00:00
Gabor Marton 25ac078a96 [clang][ASTImporter] Add isNewDecl
Summary:
Add a new function with which we can query if a Decl had been newly
created during the import process. This feature is a must if we want to
have a different static analysis strategy for such newly created
declarations.

This is a dependent patch that is needed for the new CTU implementation
discribed at
https://discourse.llvm.org/t/rfc-much-faster-cross-translation-unit-ctu-analysis-implementation/61728

Differential Revision:
https://reviews.llvm.org/D123685
2022-05-18 10:35:52 +02:00
Nico Weber e0fcdf5496 Revert "In MSVC compatibility mode, friend function declarations behave as function declarations"
This reverts commit ad47114ad8.
See discussion on https://reviews.llvm.org/D124613.
2022-05-13 09:48:01 -04:00
Aaron Ballman 2cb2cd242c Change the behavior of implicit int diagnostics
C89 allowed a type specifier to be elided with the resulting type being
int, aka implicit int behavior. This feature was subsequently removed
in C99 without a deprecation period, so implementations continued to
support the feature. Now, as with implicit function declarations, is a
good time to reevaluate the need for this support.

This patch allows -Wimplicit-int to issue warnings in C89 mode (off by
default), defaults the warning to an error in C99 through C17, and
disables support for the feature entirely in C2x. It also removes a
warning about missing declaration specifiers that really was just an
implicit int warning in disguise and other minor related cleanups.
2022-05-04 08:35:47 -04:00
Fred Tingaud ad47114ad8 In MSVC compatibility mode, friend function declarations behave as function declarations
Before C++20, MSVC treated any friend function declaration as a function declaration, so the following code would compile despite funGlob being declared after its first call:

```
class Glob {
public:
  friend void funGlob();

  void test() {
    funGlob();
  }
};

void funGlob() {}
```
This proposed patch mimics the MSVC behavior when in MSVC compatibility mode

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D124613
2022-05-03 11:31:50 +02:00
Bill Wendling 6f79700830 [randstruct] Automatically randomize a structure of function pointers
Strutures of function pointers are a good surface area for attacks. We
should therefore randomize them unless explicitly told not to.

Reviewed By: aaron.ballman, MaskRay

Differential Revision: https://reviews.llvm.org/D123544
2022-04-29 11:05:09 -07:00
Bill Wendling 463790bfc7 [randstruct] Randomize all elements of a record
A record may have more than just FieldDecls in it. If so, then we're
likely to drop them if we only randomize the FieldDecls.

We need to be careful about anonymous structs/unions. Their fields are
made available in the RecordDecl as IndirectFieldDecls, which are listed
after the anonymous struct/union. The ordering doesn't appear to be
super important, however we place them unrandomized at the end of the
RecordDecl just in case. There's also the possiblity of
StaticAssertDecls. We also want those at the end.

All other non-FieldDecls we place at the top, just in case we get
something like:

    struct foo {
      enum e { BORK };
      enum e a;
    };

Link: https://github.com/KSPP/linux/issues/185

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D123958
2022-04-28 12:01:11 -07:00
Haojian Wu 8052f4d22a [AST] Consider QualifiedTemplateName in TemplateName::getAsUsingDecl().
If the underlying template name of a qualified template name is a using
decl, TemplateName::getAsUsingDecl() will return it.

This will make the UsingTemplateName consumer life easier.

Differential Revision: https://reviews.llvm.org/D124437
2022-04-27 14:16:19 +02:00
Volodymyr Sapsai 4604db9493 [ASTStructuralEquivalence] Add support for comparing ObjCCategoryDecl.
Differential Revision: https://reviews.llvm.org/D121176
2022-04-22 16:51:19 -07:00
Tom Eccles 225b91e6cb Fix crash getting name of a template decl
NamedDecl::getIdentifier can return a nullptr when
DeclarationName::isIdentifier is false, which leads to a null pointer
dereference when TypePrinter::printTemplateId calls ->getName().

NamedDecl::getName does the same thing in the successful case and
returns an empty string in the failure case.

This crash affects the llvm 14 packages on llvm.org.
2022-04-22 13:03:28 -04:00
Nico Weber 8dbc6b5600 Revert "[randstruct] Check final randomized layout ordering"
This reverts commit a7815d33bf.
Test fails on Windows, see comments on https://reviews.llvm.org/D124199
2022-04-22 08:27:32 -04:00
Bill Wendling a7815d33bf [randstruct] Check final randomized layout ordering
This uses "llvm::shuffle" to stop differences in shuffle ordering on
different platforms.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D124199
2022-04-21 19:41:00 -07:00
Haojian Wu 1234b1c6d8 [AST] Support template declaration found through using-decl for QualifiedTemplateName.
This is a followup of https://reviews.llvm.org/D123127, adding support
for the QualifiedTemplateName.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D123775
2022-04-21 10:53:23 +02:00
Nathan James cfb8169059
[clang] Add a raw_ostream operator<< overload for QualType
Under the hood this prints the same as `QualType::getAsString()` but cuts out the middle-man when that string is sent to another raw_ostream.

Also cleaned up all the call sites where this occurs.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D123926
2022-04-20 22:09:05 +01:00
Sam McCall 4cec789c17 [Testing] Drop clangTesting from clang's public library interface
This was probably not particularly intended to be public, and disallows deps
on gtest which are useful in test helpers.

https://discourse.llvm.org/t/stop-exporting-clangtesting-library/61672

Differential Revision: https://reviews.llvm.org/D123610
2022-04-20 13:28:44 +02:00
Bill Wendling 8c77a75fb6 [randstruct] Add test for "-frandomize-layout-seed-file" flag
This test makes sure that the "-frandomize-layout-seed" and
"-frandomize-layout-seed-file" flags generate the same layout for the
record.

Reviewed By: aaron.ballman, MaskRay

Differential Revision: https://reviews.llvm.org/D123636
2022-04-14 16:35:41 -07:00
Bill Wendling 27dead3e3a Revert "[randstruct] Add test for "-frandomize-layout-seed-file" flag"
There's a test failure.

This reverts commit 31ea4798ad.
2022-04-14 16:07:00 -07:00
Bill Wendling 31ea4798ad [randstruct] Add test for "-frandomize-layout-seed-file" flag
This test makes sure that the "-frandomize-layout-seed" and
"-frandomize-layout-seed-file" flags generate the same layout for the
record.

Reviewed By: aaron.ballman, MaskRay

Differential Revision: https://reviews.llvm.org/D123636
2022-04-14 15:41:25 -07:00
Haojian Wu 6ba1b9075d Reland "[AST] Add a new TemplateKind for template decls found via a using decl.""
This is the template version of https://reviews.llvm.org/D114251.

This patch introduces a new template name kind (UsingTemplateName). The
UsingTemplateName stores the found using-shadow decl (and underlying
template can be retrieved from the using-shadow decl). With the new
template name, we can be able to find the using decl that a template
typeloc (e.g. TemplateSpecializationTypeLoc) found its underlying template,
which is useful for tooling use cases (include cleaner etc).

This patch merely focuses on adding the node to the AST.

Next steps:
- support using-decl in qualified template name;
- update the clangd and other tools to use this new node;
- add ast matchers for matching different kinds of template names;

Differential Revision: https://reviews.llvm.org/D123127
2022-04-14 11:04:55 +02:00
Balázs Kéri 596752863e [clang][ASTImporter] Fix an import error handling related bug.
This bug can cause that more import errors are generated than necessary
and many objects fail to import. Chance of an invalid AST after these
imports increases.

Reviewed By: martong

Differential Revision: https://reviews.llvm.org/D122525
2022-04-13 10:11:33 +02:00
Haojian Wu 95f0f69f1f Revert "[AST] Add a new TemplateKind for template decls found via a using decl."
It breaks arm build, there is no free bit for the extra
UsingShadowDecl in TemplateName::StorageType.

Reverting it to build the buildbot back until we comeup with a fix.

This reverts commit 5a5be4044f.
2022-04-12 11:51:00 +02:00
Haojian Wu 5a5be4044f [AST] Add a new TemplateKind for template decls found via a using decl.
This is the template version of https://reviews.llvm.org/D114251.

This patch introduces a new template name kind (UsingTemplateName). The
UsingTemplateName stores the found using-shadow decl (and underlying
template can be retrieved from the using-shadow decl). With the new
template name, we can be able to find the using decl that a template
typeloc (e.g. TemplateSpecializationTypeLoc) found its underlying template,
which is useful for tooling use cases (include cleaner etc).

This patch merely focuses on adding the node to the AST.

Next steps:
- support using-decl in qualified template name;
- update the clangd and other tools to use this new node;
- add ast matchers for matching different kinds of template names;

Differential Revision: https://reviews.llvm.org/D123127
2022-04-12 10:48:23 +02:00
Balázs Kéri 2b6e5fa62b [clang][ASTImporter] Add import of attribute 'enable_if'.
Reviewed By: martong

Differential Revision: https://reviews.llvm.org/D123397
2022-04-12 10:02:51 +02:00
Bill Wendling 77e71bcfde [randstruct] NFC change to use static 2022-04-09 13:25:25 -07:00
Connor Kuehl 7aa8c38a9e [randstruct] Add randomize structure layout support
The Randstruct feature is a compile-time hardening technique that
randomizes the field layout for designated structures of a code base.
Admittedly, this is mostly useful for closed-source releases of code,
since the randomization seed would need to be available for public and
open source applications.

Why implement it? This patch set enhances Clang’s feature parity with
that of GCC which already has the Randstruct feature. It's used by the
Linux kernel in certain structures to help thwart attacks that depend on
structure layouts in memory.

This patch set is a from-scratch reimplementation of the Randstruct
feature that was originally ported to GCC. The patches for the GCC
implementation can be found here:

  https://www.openwall.com/lists/kernel-hardening/2017/04/06/14

Link: https://lists.llvm.org/pipermail/cfe-dev/2019-March/061607.html
Co-authored-by: Cole Nixon <nixontcole@gmail.com>
Co-authored-by: Connor Kuehl <cipkuehl@gmail.com>
Co-authored-by: James Foster <jafosterja@gmail.com>
Co-authored-by: Jeff Takahashi <jeffrey.takahashi@gmail.com>
Co-authored-by: Jordan Cantrell <jordan.cantrell@mail.com>
Co-authored-by: Nikk Forbus <nicholas.forbus@gmail.com>
Co-authored-by: Tim Pugh <nwtpugh@gmail.com>
Co-authored-by: Bill Wendling <isanbard@gmail.com>
Signed-off-by: Bill Wendling <isanbard@gmail.com>

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D121556
2022-04-09 13:15:36 -07:00
Fangrui Song a58d0af058 Revert D121556 "[randstruct] Add randomize structure layout support"
This reverts commit 3f0587d0c6.

Not all tests pass after a few rounds of fixes.

I spot one failure that std::shuffle (potentially different results with
different STL implementations) was misused and replaced it with llvm::shuffle,
but there appears to be another failure in a Windows build.

The latest failure is reported on https://reviews.llvm.org/D121556#3440383
2022-04-08 18:37:26 -07:00
Fangrui Song 46b2a463bd [randstruct] Use llvm::shuffle to avoid STL impl difference after D121556
This reverts commit 2a2149c754.
This reverts commit 8d7595be1d.
This reverts commit e2e6899452.

If this doesn't work, I'll revert the whole thing.
2022-04-08 18:14:21 -07:00
Bill Wendling 2a2149c754 [randstruct] Remove RandstructTest.cpp from list 2022-04-08 17:41:36 -07:00
Bill Wendling 8d7595be1d [randstruct] temporarily remove test that's failing 2022-04-08 17:04:22 -07:00
Bill Wendling e2e6899452 [randstruct] disable test for Windows for now. 2022-04-08 16:00:41 -07:00
Bill Wendling 893e1c18b9 [randstruct] add expected output for WIN64
This is an attempt to fix a test failure on one of the buildbot Windows
machines. It also turns all of the "ASSERT_" macros into "EXPECT_" to
catch all other failures.

Link: https://lab.llvm.org/buildbot/#/builders/216/builds/2647
2022-04-08 14:42:12 -07:00
Connor Kuehl 3f0587d0c6 [randstruct] Add randomize structure layout support
The Randstruct feature is a compile-time hardening technique that
randomizes the field layout for designated structures of a code base.
Admittedly, this is mostly useful for closed-source releases of code,
since the randomization seed would need to be available for public and
open source applications.

Why implement it? This patch set enhances Clang’s feature parity with
that of GCC which already has the Randstruct feature. It's used by the
Linux kernel in certain structures to help thwart attacks that depend on
structure layouts in memory.

This patch set is a from-scratch reimplementation of the Randstruct
feature that was originally ported to GCC. The patches for the GCC
implementation can be found here:

  https://www.openwall.com/lists/kernel-hardening/2017/04/06/14

Link: https://lists.llvm.org/pipermail/cfe-dev/2019-March/061607.html
Co-authored-by: Cole Nixon <nixontcole@gmail.com>
Co-authored-by: Connor Kuehl <cipkuehl@gmail.com>
Co-authored-by: James Foster <jafosterja@gmail.com>
Co-authored-by: Jeff Takahashi <jeffrey.takahashi@gmail.com>
Co-authored-by: Jordan Cantrell <jordan.cantrell@mail.com>
Co-authored-by: Nikk Forbus <nicholas.forbus@gmail.com>
Co-authored-by: Tim Pugh <nwtpugh@gmail.com>
Co-authored-by: Bill Wendling <isanbard@gmail.com>
Signed-off-by: Bill Wendling <isanbard@gmail.com>

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D121556
2022-04-08 12:48:30 -07:00
Iain Sandoe c7ed65b4bc [C++20][Modules] Limit ModuleInternalLinkage to modules-ts.
At present, we are generating wrong code for C++20 modules entities which
should have internal linkage.  This is because we are assigning
'ModuleInternalLinkage' unconditionally to such entities.  However this mode
is only applicable to the modules-ts.

This change makes the special linkage mode conditional on fmodules-ts and
adds a unit test to verify that we generate the correct linkage.

Currently, static variables and functions in module purview are emitted into
object files as external. On some platforms, lambdas are emitted as global
weak defintions (on Windows this causes a mangler crash).

Differential Revision: https://reviews.llvm.org/D122413
2022-04-01 09:10:30 +01:00
Balázs Kéri c5d83cdca4 [clang][ASTImporter] Fix a bug when importing CXXDefaultInitExpr.
The "in-class initializer" expression should be set in the field of a
default initialization expression before this expression node is created.
The `CXXDefaultInitExpr` objects are created after the AST is loaded and
at import not present in the "To" AST. And the in-class initializers of
the used fields can be missing too, these must be set at import.

This fixes a github issue #54061.

Reviewed By: martong

Differential Revision: https://reviews.llvm.org/D120824
2022-03-28 10:55:26 +02:00
Corentin Jabot 3784e8ccfb [Clang] Fix Unevaluated Lambdas
Unlike other types, when lambdas are instanciated,
they are recreated from scratch.
When an unevaluated lambdas appear in the type of a function,
parameter it is instanciated in the wrong declaration context,
as parameters are transformed before the function.

To support lambda in function parameters, we try to
compute whether they are dependant without looking at the
declaration context.

This is a short term stopgap solution to avoid clang
iceing. A better fix might be to inject some kind of
transparent declaration with correctly computed dependency
for function parameters, variable templates, etc.

Fixes https://github.com/llvm/llvm-project/issues/50376
Fixes https://github.com/llvm/llvm-project/issues/51414
Fixes https://github.com/llvm/llvm-project/issues/51416
Fixes https://github.com/llvm/llvm-project/issues/51641
Fixes https://github.com/llvm/llvm-project/issues/54296

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D121532
2022-03-25 19:16:45 +01:00
Kai Luo b8388fa319 [clang][NFC] Fix warning of integer comparison
```
warning: comparison of integers of different signs: 'const unsigned long' and 'const int' [-Wsign-compare]
```

Fix https://lab.llvm.org/buildbot/#/builders/57/builds/16220.
2022-03-24 14:06:45 +08:00
Chuanqi Xu 8474668608 [C++20] [Modules] Make the linkage consistent for template and its
specialization

Before the patch, the compiler would crash for the test due to
inconsistent linkage.

This patch tries to avoid it by make the linkage consistent for template
and its specialization. After the patch, the behavior of compiler would
be partially correct for the case.
The correct one is:

```
export template<class T>
void f() {}

template<>
void f<int>() {}
```

In this case, the linkage for both declaration should be external (the
wording I get by consulting in WG21 is "the linkage for name f should be
external").

And for the case:
```
template<class T>
void f() {}

export template<>
void f<int>() {}
```

Compiler should reject it. This isn't done now. After all, this patch would
stop a crash.

Reviewed By: iains, aaron.ballman, dblaikie

Differential Revision: https://reviews.llvm.org/D120397
2022-03-24 10:24:14 +08:00
Zhihao Yuan 44eee659f1
[AST] Print NTTP args as string-literals when possible
C++20 non-type template parameter prints `MyType<{{116, 104, 105, 115}}>` when the code is as simple as `MyType<"this">`. This patch prints `MyType<{"this"}>`, with one layer of braces preserved for the intermediate structural type to trigger CTAD.

`StringLiteral` handles this case, but `StringLiteral` inside `APValue` code looks like a circular dependency. The proposed patch implements a cheap strategy to emit string literals in diagnostic messages only when they are readable and fall back to integer sequences.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D115031
2022-03-01 19:34:27 -06:00
Chuanqi Xu f85a6a8127 [NFC] Add unittest for Decl::isInExportDeclContext 2022-02-23 16:29:42 +08:00
David Goldman 805f7a4fa4 [clang] Add `ObjCProtocolLoc` to represent protocol references
Add `ObjCProtocolLoc` which behaves like `TypeLoc` but for
`ObjCProtocolDecl` references.

RecursiveASTVisitor now synthesizes `ObjCProtocolLoc` during traversal
and the `ObjCProtocolLoc` can be stored in a `DynTypedNode`.

In a follow up patch, I'll update clangd to make use of this
to properly support protocol references for hover + goto definition.

Differential Revision: https://reviews.llvm.org/D119363
2022-02-18 15:24:00 -05:00