72140 Commits

Author SHA1 Message Date
Jonas Hahnfeld
3116d60494 [Lex] Introduce Preprocessor::LexTokensUntilEOF()
This new method repeatedly calls Lex() until end of file is reached
and optionally fills a std::vector of Tokens. Use it in Clang's unit
tests to avoid quite some code duplication.

Differential Revision: https://reviews.llvm.org/D158413
2023-10-05 11:04:07 +02:00
cor3ntin
49666ec038
[Clang] Fix constant evaluating a captured variable in a lambda (#68090)
with an explicit parameter.

We tried to read a pointer to a non-existent `This` APValue when
constant-evaluating an explicit object lambda call operator (the `this`
pointer is never set in explicit object member functions)

Fixes #68070
2023-10-05 10:17:50 +02:00
Timm Bäder
57147bb253 [clang][Interp] Support LambdaThisCaptures
Differential Revision: https://reviews.llvm.org/D154262
2023-10-05 09:46:15 +02:00
Timm Bäder
4d7f4a7c82 [clang][Interp] Only lazily visit constant globals
Differential Revision: https://reviews.llvm.org/D158516
2023-10-05 09:37:37 +02:00
Timm Baeder
5ef904b5da
[clang][ExprConst] Don't try to evaluate value-dependent DeclRefExprs (#67778)
The Expression here migth be value dependent, which makes us run into an
assertion later on. Just bail out early.

Fixes #67690
2023-10-05 08:42:34 +02:00
Yaxun (Sam) Liu
c6ed5a6125 Revert "[HIP] Support compressing device binary (#67162)"
This reverts commit a1e81d2ead02e041471ec2299d7382f80c4dbba6.

Revert "Fix test hip-offload-compress-zlib.hip"

This reverts commit ba01ce60665848478ba4e76190907153a8c26fe9.

Revert due to sanity fail at

https://lab.llvm.org/buildbot/#/builders/5/builds/37188

https://lab.llvm.org/buildbot/#/builders/238/builds/5955

/b/sanitizer-aarch64-linux-bootstrap-ubsan/build/llvm-project/clang/lib/Driver/OffloadBundler.cpp:1012:25: runtime error: load of misaligned address 0xaaaae2d90e7c for type 'const uint64_t' (aka 'const unsigned long'), which requires 8 byte alignment
0xaaaae2d90e7c: note: pointer points here
  bc 00 00 00 94 dc 29 9a  89 fb ca 2b 78 9c 8b 8f  77 f6 71 f4 73 8f f7 77  73 f3 f1 77 74 89 77 0a
              ^
    #0 0xaaaaba125f70 in clang::CompressedOffloadBundle::decompress(llvm::MemoryBuffer const&, bool) /b/sanitizer-aarch64-linux-bootstrap-ubsan/build/llvm-project/clang/lib/Driver/OffloadBundler.cpp:1012:25
    #1 0xaaaaba126150 in clang::OffloadBundler::ListBundleIDsInFile(llvm::StringRef, clang::OffloadBundlerConfig const&) /b/sanitizer-aarch64-linux-bootstrap-ubsan/build/llvm-project/clang/lib/Driver/OffloadBundler.cpp:1089:7

Will reland after fixing it.
2023-10-05 00:29:42 -04:00
Bill Wendling
9a954c6935 [Clang] Implement the 'counted_by' attribute
The 'counted_by' attribute is used on flexible array members. The
argument for the attribute is the name of the field member in the same
structure holding the count of elements in the flexible array. This
information can be used to improve the results of the array bound sanitizer
and the '__builtin_dynamic_object_size' builtin.

This example specifies the that the flexible array member 'array' has the
number of elements allocated for it in 'count':

  struct bar;
  struct foo {
    size_t count;
     /* ... */
    struct bar *array[] __attribute__((counted_by(count)));
  };

This establishes a relationship between 'array' and 'count', specifically
that 'p->array' must have *at least* 'p->count' number of elements available.
It's the user's responsibility to ensure that this relationship is maintained
through changes to the structure.

In the following, the allocated array erroneously has fewer elements than
what's specified by 'p->count'. This would result in an out-of-bounds access not
not being detected:

  struct foo *p;

  void foo_alloc(size_t count) {
    p = malloc(MAX(sizeof(struct foo),
                   offsetof(struct foo, array[0]) + count *
                       sizeof(struct bar *)));
    p->count = count + 42;
  }

The next example updates 'p->count', breaking the relationship requirement that
'p->array' must have at least 'p->count' number of elements available:

  struct foo *p;

  void foo_alloc(size_t count) {
    p = malloc(MAX(sizeof(struct foo),
                   offsetof(struct foo, array[0]) + count *
                       sizeof(struct bar *)));
    p->count = count + 42;
  }

  void use_foo(int index) {
    p->count += 42;
    p->array[index] = 0; /* The sanitizer cannot properly check this access */
  }

Reviewed By: nickdesaulniers, aaron.ballman

Differential Revision: https://reviews.llvm.org/D148381
2023-10-04 18:26:15 -07:00
Matheus Izvekov
6da382d27b
[Clang][Driver] Add new flags to control IR verification (#68172)
Enables or disables verification of the generated LLVM IR.

Users can pass this to turn on extra verification to catch certain types of
compiler bugs at the cost of extra compile time.
2023-10-05 00:50:30 +02:00
Ian Anderson
4ee8c676ee
[Modules] no_undeclared_includes modules (Apple Darwin) don't work the clang modules (#68241)
All of the _Builtin_stdarg and _Builtin_stddef submodules need to be
allowed from [no_undeclared_includes] modules. Split the builtin headers
tests out from the compiler_builtins test so that the testing modules
can be modified without affecting the other many tests that use
Inputs/System/usr/include.
2023-10-04 15:43:17 -07:00
Matheus Izvekov
5099dc341f
[Clang][CodeGen] Fix use of CXXThisValue with StrictVTablePointers (#68169) 2023-10-04 23:41:49 +02:00
Björn Schäpers
b6f29191ad
[clang-format][NFC] AlignTokenSequence: Skip loop iteration
When Shift is 0 there does nothing happen in the remainder of the loop,
express that directly.
2023-10-04 22:20:33 +02:00
Björn Schäpers
7539bcf994
[clang-format][NFC] AlignTokenSequence: Rename Changes[i] to CurrentC…
…hange

To improve debugging experience.
2023-10-04 21:19:56 +02:00
Björn Schäpers
b4a076a12d
[clang-format][NFC] AlignTokens: Rename Changes[i] to CurrentChange (#68152)
To improve debugging experience.
2023-10-04 21:19:21 +02:00
Pranav Kant
7d21086d0c Revert "[clang] Predefined macros for float128 support (#67196)"
This reverts commit 457f582ffe23e951380bc345c4c96ec053c09681.
2023-10-04 18:19:24 +00:00
Botond István Hprváth
66c19167f1
[clang] Choose non-templated ctor as deduction guide unambiguously (#66487)
If there are two guides, one of them generated from a non-templated
constructor
and the other from a templated constructor, then the standard gives
priority to
the first. Clang detected ambiguity before, now the correct guide is
chosen.
The correct behavior is described in this paper:
http://wg21.link/P0620R0

Example for the bug: http://godbolt.org/z/ee3e9qG78

As an unrelated minor change, fix the issue
https://github.com/llvm/llvm-project/issues/64020,
which could've led to incorrect behavior if further development inserted
code after a call to
`isAddressSpaceSubsetOf()`, which specified the two parameters in the
wrong order.

---------

Co-authored-by: hobois <horvath.botond.istvan@gmial.com>
2023-10-04 09:11:43 -07:00
Yaxun (Sam) Liu
a1e81d2ead
[HIP] Support compressing device binary (#67162)
Add option -f[no-]offload-compress to clang to enable/disable
compression of device binary for HIP. By default it is disabled.

Add option -compress to clang-offload-bundler to enable compression of
offload bundle. By default it is disabled.

When enabled, zstd or zlib is used for compression when available.

When disabled, it is NFC compared to previous behavior. The same offload
bundle format is used as before.

Clang-offload-bundler automatically detects whether the input file to be
unbundled is compressed and the compression method and decompress if
necessary.
2023-10-04 09:32:56 -04:00
Nikita Popov
39d55321bd
[CodeGen] Respect pointer-overflow sanitizer for void pointers (#67772)
Pointer arithmetic on void pointers (a GNU extension) was going through
a different code path and bypassed the pointer-overflow sanitizer.

Fixes https://github.com/llvm/llvm-project/issues/66451.
2023-10-04 15:16:00 +02:00
martinboehme
2be7c651c4
[clang][dataflow] HTML logger: Mark iterations that have converged. (#68204)
I've eliminated the `logText("Block converged")` call entirely because

a) These logs are associated with an individual `CFGElement`, while
convergence
   should be associated with a block, and

b) The log message was being associated with the wrong block:
`recordState()`
dumps all of the pending log messages, but `blockConverged()` is called
after
   the last `recordState()` call for a given block, so that the "Block
converged" log message was being associated with the first element of
the
   _next_ block to be processed.

Example:


![image](https://github.com/llvm/llvm-project/assets/29098113/6a19095c-2dbb-4771-9485-e8e45c9d26fb)
2023-10-04 13:47:24 +02:00
zyn0217
077e1b892d
[clang] Preserve UDL nodes in RemoveNestedImmediateInvocation (#66641)
D63960 performs a tree transformation on AST to prevent evaluating
constant expressions eagerly by removing recorded immediate consteval
invocations from subexpressions. (See
https://reviews.llvm.org/D63960#inline-600736 for its motivation.)

However, the UDL node has been replaced with a CallExpr in the default
TreeTransform since ca844ab0 (inadvertently?). This confuses clangd as
it relies on the exact AST node type to decide whether or not to present
inlay hints for an expression.

With regard to the fix, I think it's enough to return the UDL expression
as-is during the transformation: We've bound it to temporary in its
construction, and there's no ConstantExpr to visit under a UDL.

Fixes https://github.com/llvm/llvm-project/issues/63898.
2023-10-04 04:45:46 -05:00
Owen Pan
7e856d1894 Reland "[clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (#67955)"
Reland 6a621ed8e4cb which failed on Windows but not macOS.

The failures were caused by moving the annotation of the children from the
top to the bottom of TokenAnnotator::annotate(), resulting in invalid lines
including incomplete ones being skipped.
2023-10-03 22:26:48 -07:00
Takuya Shimizu
2176c5e510 [Clang][Sema] Fix display of characters on static assertion failure
This patch fixes the display of characters appearing in LHS or RHS of == expression in notes to static assertion failure.
This applies C-style escape if the printed character is a special character. This also adds a numerical value displayed next to the character representation.
This also tries to print multi-byte characters if the user-provided expression is multi-byte char type.

Reviewed By: cor3ntin
Differential Revision: https://reviews.llvm.org/D155610
2023-10-04 14:09:06 +09:00
Sheng
548d67a039
[clang][Sema] Fix a bug when instantiating a lambda with requires clause (#65193)
Instantiating a lambda at a scope different from where it is defined
will paralyze clang if the trailing require clause refers to local
variables. This patch fixes this by re-adding the local variables to
`LocalInstantiationScope`.

Fixes #64462
2023-10-04 10:19:35 +08:00
Owen Pan
d08fcc817e Revert "[clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (#67955)"
This reverts commit 6a621ed8e4cb02bd55fe4a4a0254615576b70a55 as it caused
buildbots to fail.
2023-10-03 18:19:23 -07:00
Owen Pan
6a621ed8e4
[clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (#67955)
After annotating constructors/destructors as FunctionDeclarationName in
commit 08630512088, we have seen several issues because ctors/dtors had
been treated differently than functions in aligning, wrapping, and
indenting.

This patch annotates ctors/dtors as CtorDtorDeclName instead and would
effectively revert commit 0468fa07f87f, which is obsolete now.

Fixed #67903.
Fixed #67907.
2023-10-03 18:02:09 -07:00
tripleCC
1dceba3a36
[analyzer] Fix false negative when accessing a nonnull property from … (#67563)
```
@interface A : NSObject
@property (nonnull, nonatomic, strong) NSString *name;
+ (nullable instancetype)shared;
@end

@[[A shared].name];
```
Consider the code above, the nullability of the name property should
depend on the result of the shared method. A warning is expected because
of adding a nullable object to array.
ObjCMessageExpr gets the actual type through
Sema::getMessageSendResultType, instead of using the return type of
MethodDecl directly. The final type is generated by considering the
nullability of receiver and MethodDecl together.
Thus, the RetType in NullabilityChecker should all be replaced with
M.getOriginExpr()->getType().

Co-authored-by: tripleCC <triplecc@gmail.com>
2023-10-04 08:38:10 +08:00
PiJoules
496d00b267
[clang][RelativeVTables] Make the rtti_proxy LinkOnceODR instead of External linkage (#67755)
This way, it the rtti_proxies can be candidates for being replaced
altogether with GOTPCREL relocations because they are discardable.
Functionally, this shouldn't change the final ELF linkage of the
proxies.
2023-10-03 17:05:21 -07:00
Jan Svoboda
27254ae511
[clang] NFCI: Use FileEntryRef for FileID creation (#67838)
This patch removes the `SourceManager` APIs that create `FileID` from a
`const FileEntry *` in favor of APIs that take `FileEntryRef`. This also
removes a misleading documentation that claims `nullptr` file entry
represents stdin. I don't think that's right, since we just try to
dereference that pointer anyways.
2023-10-03 13:07:46 -07:00
Shoaib Meenai
c6fed74f6f
[diag] Silence -Wfixed-enum-extension in C23 (#68060)
The C23 standard supports enums with fixed underlying types (N3030 [1]),
so we shouldn't emit `-Wfixed-enum-extension` in C23 mode (since it's no
longer a Clang extension at that point).

[1]
https://thephd.dev/_vendor/future_cxx/papers/C%20-%20Enhanced%20Enumerations.html
2023-10-03 13:04:22 -07:00
Ian Anderson
9a7a6dd3c3 [Modules] Make clang modules for the C standard library headers
Make top level modules for all the C standard library headers.

The `__stddef` implementation headers need header guards now that they're all modular. stdarg.h and stddef.h will be textual headers in the builtin modules, and so need to be repeatedly included in both the system and builtin module case. Define their header guards for consistency, but ignore them when building with modules.

`__stddef_null.h` needs to ignore its header guard when modules aren't being used to fulfill its redefinition obligation.
`__stddef_nullptr_t.h` needs to add a guard for C23 so that `_Builtin_stddef` can compile in C17 and earlier modes. `_Builtin_stddef.nullptr_t` can't require C23 because it also needs to be usable from C++.

Reviewed By: Bigcheese

Differential Revision: https://reviews.llvm.org/D159064
2023-10-03 12:41:11 -07:00
Nouman Amir
af16a4e131 Improve error message for constexpr constructors of virtual base classes
The changes are for better diagnostic/error-messages. The error message
of Clang, MSVC, and GCC were compared and MSVC gives more detailed
error message so that is used now.

Fixes https://github.com/llvm/llvm-project/issues/64843
Differential Revision: https://reviews.llvm.org/D158540
2023-10-03 15:06:08 -04:00
Kazu Hirata
f659ef43cd [AST] Fix a warning
This patch fixes:

  clang/lib/AST/ASTContext.cpp:931:40: error: field
  'DependentBitIntTypes' will be initialized after field
  'FunctionProtoTypes' [-Werror,-Wreorder-ctor]
2023-10-03 11:08:24 -07:00
cor3ntin
9c1c221169
[Clang] Correctly handle callees whose type is a Record type when classifying expressions (#68078)
when the callee is an object.

When implementing deducing this, we changed
`DeduceTemplateArgumentsFromCallArgument` to take an argument
classification because we need to deduce the type of argument for which
we might not have an expression yet.

However classifying a dependent call expression whose type is just some
sort of record or elaborated type was not supported.

Fixes #68024
2023-10-03 19:38:45 +02:00
Matheus Izvekov
6d209bd436
[NFC][clang] change remaining context-dependent type nodes to ContextualFoldingSet (#67751)
With this change, we are avoiding storing one pointer per Type Node
instance, for the cost of one extra pointer per ASTContext, which is
negligible.

After we introduced `ContextualFoldingSet` a long time ago, we never
went back and updated these existing users.
This patch corrects that.
2023-10-03 19:00:10 +02:00
Matheus Izvekov
1ab68d708a
[clang][codegen] Add a verifier IR pass before any further passes. (#68015)
This helps check clang generated good IR in the first place, as
otherwise this can cause UB in subsequent passes, with the final
verification pass not catching any issues.

This for example would have helped catch
https://github.com/llvm/llvm-project/issues/67937 earlier.
2023-10-03 18:05:54 +02:00
David Stone
06a05f3346
[NFC] remove duplicate ModuleId alias (#67899)
[clang] Remove duplicate `ModuleId` alias
2023-10-03 22:43:51 +08:00
Björn Schäpers
1f01269028
[clang-format] Fix alignment in presence of template functions (#68029)
Fixes #68102.
2023-10-03 15:35:30 +02:00
agozillon
bc0c1783fd
[Clang][OpenMP][OMPIRBuilder] Move Clang's OpenMP Member/MemberOf flag helpers into the OMPIRBuilder (#67844)
This patch seeks to move the following functions to the OMPIRBuilder:
 - getFlagMemberOffset
 - getMemberOfFlag
 - setCorrectMemberOfFlag

These small helper functions help set the end bits of the
OpenMPOffloadMappingFlags flag that correspond to the reserved segment
for OMP_MAP_MEMBER_OF.

They will be of use in the future for lowering MLIR types/values that
can contian members and can be lowered similarly to a structure or class
type within the OpenMPToLLVMIRTranslation step of the OpenMP dialects
lowering to LLVM-IR. In particular for Flang which currently uses this
flow. Types with descriptors like pointers/allocatables, and likely
derived types in certain cases can be lowered as if they were structures
with explicitly mapped members.
2023-10-03 15:20:44 +02:00
Dhanrajbir-Hira
0bb4b24ca3
[clang][AIX]Fix -flto-jobs for AIX. (#67853)
Currently using the `-flto-jobs=N` option passes `-bplugin_opt:jobs=N`
to the AIX linker which is not a valid option. This PR seeks to change
this behaviour to instead pass `-bplugin_opt:-threads=N` to control the
level of concurrency during LTO builds.
2023-10-03 09:18:21 -04:00
Alex Voicu
4d680f5647 [HIP][Clang][Sema] Add Sema support for hipstdpar
This patch adds the Sema changes needed for enabling HIP parallel algorithm offload on AMDGPU targets. This change impacts the CUDA / HIP language specific checks, and only manifests if compiling in `hipstdpar` mode. In this case, we essentially do three things:

1. Allow device side callers to call host side callees - since the user visible HLL would be standard C++, with no annotations / restriction mechanisms, we cannot unambiguously establish that such a call is an error, so we conservatively allow all such calls, deferring actual cleanup to a subsequent pass over IR;
2. Allow host formed lambdas to capture by reference;
3. Allow device functions to use host global variables.

Reviewed by: yaxunl

Differential Revision: https://reviews.llvm.org/D155833
2023-10-03 13:29:12 +01:00
Alex Voicu
c0f8748d44 [HIP][Clang][Preprocessor] Add Preprocessor support for hipstdpar
This patch adds the Driver changes needed for enabling HIP parallel algorithm offload on AMDGPU targets. This change merely adds two macros to inform user space if we are compiling in `hipstdpar` mode and, respectively, if the optional allocation interposition mode has been requested, as well as associated minimal tests. The macros can be used by the runtime implementation of offload to drive conditional compilation, and are only defined if the HIP language has been enabled.

Reviewed by: yaxunl

Differential Revision: https://reviews.llvm.org/D155826
2023-10-03 13:18:31 +01:00
Alex Voicu
9a408588d1 [HIP][Clang][Driver] Add Driver support for hipstdpar
This patch adds the Driver changes needed for enabling HIP parallel algorithm offload on AMDGPU targets. What this change does can be summed up as follows:

- add two flags, one for enabling `hipstdpar` compilation, the second enabling the optional allocation interposition mode;
- the flags correspond to new LangOpt members;
- if we are compiling or linking with --hipstdpar, we enable HIP; in the compilation case C and C++ inputs are treated as HIP inputs;
- the ROCm / AMDGPU driver is augmented to look for and include an implementation detail forwarding header; we error out if the user requested `hipstdpar` but the header or its dependencies cannot be found.

Tests for the behaviour described above are also added.

Reviewed by: MaskRay, yaxunl

Differential Revision: https://reviews.llvm.org/D155775
2023-10-03 13:14:46 +01:00
Jonas Hahnfeld
05137ecfca
[clang-repl] Emit const variables only once (#65257)
Disable internal linkage for const variables if IncrementalExtensions
are enabled. Otherwise the variables are emitted multiple times, with
multiple constructions at unique memory locations, during every PTU.
2023-10-03 11:58:23 +02:00
Timm Baeder
f58d54ab96
[clang][Interp] Diagnose uninitialized bases (#67131) 2023-10-03 06:46:31 +02:00
Timm Bäder
fb0a7c8589 [clang][Interp][NFC] Make IntegralAP::V private 2023-10-03 06:31:03 +02:00
Timm Baeder
ef9666aa81
[clang][Interp] Fix value truncation when casting int128 to smaller size (#67961)
Before this patch, we would run into an assertion in
`APInt::get{S,Z}ExtValue()` because the `AllOnes` value had more than 64
active bits.
2023-10-03 06:05:39 +02:00
Matheus Izvekov
e8970141ec
[NFC][Clang][CodeGen] Improve performance for vtable metadata generation (#67066)
Mangle each AddressPoint once, instead of once per comparison.
2023-10-03 03:23:34 +02:00
Kirill Stoimenov
1493462868 Revert "[NFC][Clang][CodeGen] Improve performance for vtable metadata generation (#67066)"
This reverts commit 22d8f1dd533e3e56512237811b8d8db83d85edce.

Broke sanitizer bots: https://lab.llvm.org/buildbot/#/builders/269/builds/59
2023-10-03 00:09:46 +00:00
Matheus Izvekov
22d8f1dd53
[NFC][Clang][CodeGen] Improve performance for vtable metadata generation (#67066) 2023-10-03 00:45:00 +02:00
Pranav Kant
457f582ffe
[clang] Predefined macros for float128 support (#67196)
Expose some predefined macros for Float128 support.
2023-10-02 14:14:52 -07:00
Akira Hatanaka
1925c7aa37
Mark an ObjCIvarDecl as invalid if its type contains errors (#68001)
This fixes an assertion failure in InitializationSequence::Perform.
2023-10-02 12:46:00 -07:00