llvm-project/clang/test
George Karpenkov ec38cf7aed [ast] Do not auto-initialize Objective-C for-loop variables in Objective-C++ in templatized code under ARC
The AST for the fragment

```
@interface I
@end

template <typename>
void decode(I *p) {
  for (I *k in p) {}
}

void decode(I *p) {
  decode<int>(p);
}
```

differs heavily when templatized and non-templatized:

```
|-FunctionTemplateDecl 0x7fbfe0863940 <line:4:1, line:7:1> line:5:6 decode
| |-TemplateTypeParmDecl 0x7fbfe0863690 <line:4:11> col:11 typename depth 0 index 0
| |-FunctionDecl 0x7fbfe08638a0 <line:5:1, line:7:1> line:5:6 decode 'void (I *__strong)'
| | |-ParmVarDecl 0x7fbfe08637a0 <col:13, col:16> col:16 referenced p 'I *__strong'
| | `-CompoundStmt 0x7fbfe0863b88 <col:19, line:7:1>
| |   `-ObjCForCollectionStmt 0x7fbfe0863b50 <line:6:3, col:20>
| |     |-DeclStmt 0x7fbfe0863a50 <col:8, col:13>
| |     | `-VarDecl 0x7fbfe08639f0 <col:8, col:11> col:11 k 'I *const __strong'
| |     |-ImplicitCastExpr 0x7fbfe0863a90 <col:16> 'I *' <LValueToRValue>
| |     | `-DeclRefExpr 0x7fbfe0863a68 <col:16> 'I *__strong' lvalue ParmVar 0x7fbfe08637a0 'p' 'I *__strong'
| |     `-CompoundStmt 0x7fbfe0863b78 <col:19, col:20>
| `-FunctionDecl 0x7fbfe0863f80 <line:5:1, line:7:1> line:5:6 used decode 'void (I *__strong)'
|   |-TemplateArgument type 'int'
|   |-ParmVarDecl 0x7fbfe0863ef8 <col:13, col:16> col:16 used p 'I *__strong'
|   `-CompoundStmt 0x7fbfe0890cf0 <col:19, line:7:1>
|     `-ObjCForCollectionStmt 0x7fbfe0890cc8 <line:6:3, col:20>
|       |-DeclStmt 0x7fbfe0890c70 <col:8, col:13>
|       | `-VarDecl 0x7fbfe0890c00 <col:8, col:11> col:11 k 'I *__strong' callinit
|       |   `-ImplicitValueInitExpr 0x7fbfe0890c60 <<invalid sloc>> 'I *__strong'
|       |-ImplicitCastExpr 0x7fbfe0890cb0 <col:16> 'I *' <LValueToRValue>
|       | `-DeclRefExpr 0x7fbfe0890c88 <col:16> 'I *__strong' lvalue ParmVar 0x7fbfe0863ef8 'p' 'I *__strong'
|       `-CompoundStmt 0x7fbfe0863b78 <col:19, col:20>
```

Note how in the instantiated version ImplicitValueInitExpr unexpectedly appears.

While objects are auto-initialized under ARC, it does not make sense to
have an initializer for a for-loop variable, and it makes even less
sense to have such a different AST for instantiated and non-instantiated
version.

Digging deeper, I have found that there are two separate Sema* files for
dealing with templates and for dealing with non-templatized code.
In a non-templatized version, an initialization was performed only for
variables which are not loop variables for an Objective-C loop and not
variables for a C++ for-in loop:

```
  if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
    bool IsForRangeLoop = false;
    if (TryConsumeToken(tok::colon, FRI->ColonLoc)) {
      IsForRangeLoop = true;
      if (Tok.is(tok::l_brace))
        FRI->RangeExpr = ParseBraceInitializer();
      else
        FRI->RangeExpr = ParseExpression();
    }

    Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
    if (IsForRangeLoop)
      Actions.ActOnCXXForRangeDecl(ThisDecl);
    Actions.FinalizeDeclaration(ThisDecl);
    D.complete(ThisDecl);
    return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
  }

  SmallVector<Decl *, 8> DeclsInGroup;
  Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(
      D, ParsedTemplateInfo(), FRI);
```

However the code in SemaTemplateInstantiateDecl was inconsistent,
guarding only against C++ for-in loops.

rdar://38391075

Differential Revision: https://reviews.llvm.org/D44989

llvm-svn: 328749
2018-03-29 00:56:24 +00:00
..
ARCMT [ObjC] Allow declaring __strong pointer fields in structs in Objective-C 2018-02-28 07:15:55 +00:00
ASTMerge
Analysis [analyzer] LoopUnrolling: update the matched assignment operators 2018-03-27 12:16:56 +00:00
CXX [C++17] Fix class template argument deduction for default constructors without an initializer 2018-03-24 04:32:11 +00:00
CodeCompletion [SemaOverload] Fixed crash on code completion 2018-03-09 14:43:29 +00:00
CodeGen [Hexagon] Add support for "new" circular buffer intrinsics 2018-03-28 19:40:57 +00:00
CodeGenCUDA Really fix test on windows. 2018-02-23 19:38:41 +00:00
CodeGenCXX [ObjC++] Make parameter passing and function return compatible with ObjC 2018-03-28 21:13:14 +00:00
CodeGenCoroutines [MS] Don't escape MS C++ names with \01 2018-03-16 20:36:49 +00:00
CodeGenObjC [CodeGen] Ignore OpaqueValueExprs that are unique references to their 2018-03-20 01:47:58 +00:00
CodeGenObjCXX [ObjC++] Make parameter passing and function return compatible with ObjC 2018-03-28 21:13:14 +00:00
CodeGenOpenCL AMDGPU: Update datalayout for stack alignment 2018-03-27 19:26:51 +00:00
Coverage
CoverageMapping
Driver [PATCH] [RISCV] Verify the input value of -march= 2018-03-28 08:29:50 +00:00
FixIt
Format
Frontend [Attr] Merge two dependent tests from different directories 2018-03-13 22:18:29 +00:00
Headers
Import
Index [vfs] Don't bail out after a missing -ivfsoverlay file 2018-03-23 17:37:27 +00:00
Integration
Layout
Lexer [ObjC] Allow declaring __strong pointer fields in structs in Objective-C 2018-02-28 07:15:55 +00:00
Misc Adding nocf_check attribute for cf-protection fine tuning 2018-03-17 13:31:35 +00:00
Modules [ODRHash] Support pointer and reference types. 2018-03-24 00:52:44 +00:00
OpenMP [OPENMP] Codegen for ctor|dtor of declare target variables. 2018-03-28 14:28:54 +00:00
PCH [MS] Fix late-parsed template infinite loop in eager instantiation 2018-03-26 18:22:47 +00:00
Parser [C++17] Fix class template argument deduction for default constructors without an initializer 2018-03-24 04:32:11 +00:00
Preprocessor Fix some handling of AST nodes with diagnostics. 2018-03-28 04:16:13 +00:00
Profile
Refactor
Rewriter
Sema [Diag] Avoid emitting a redefinition note if no location is available. 2018-03-28 16:05:05 +00:00
SemaCUDA Fix some handling of AST nodes with diagnostics. 2018-03-28 04:16:13 +00:00
SemaCXX Fix some handling of AST nodes with diagnostics. 2018-03-28 04:16:13 +00:00
SemaObjC [ast] Do not auto-initialize Objective-C for-loop variables in Objective-C++ in templatized code under ARC 2018-03-29 00:56:24 +00:00
SemaObjCXX
SemaOpenCL
SemaTemplate Fix some handling of AST nodes with diagnostics. 2018-03-28 04:16:13 +00:00
TableGen TableGen: Give up on exact fixits for diagnostic groups 2018-03-06 17:55:00 +00:00
Templight
Tooling
Unit
VFS [vfs] Don't bail out after a missing -ivfsoverlay file 2018-03-23 17:37:27 +00:00
clang-rename
.clang-format
CMakeLists.txt
TestRunner.sh
cxx-sections.data
lit.cfg.py
lit.site.cfg.py.in
make_test_dirs.pl