2006-11-10 12:58:55 +08:00
|
|
|
//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
|
2006-08-17 13:51:27 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-08-17 13:51:27 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2006-11-10 12:58:55 +08:00
|
|
|
// This file implements the actions class which performs semantic analysis and
|
|
|
|
// builds an AST out of a parse stream.
|
2006-08-17 13:51:27 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-08-26 10:13:20 +08:00
|
|
|
#include "clang/Sema/SemaInternal.h"
|
2006-11-10 14:20:45 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2010-02-10 06:26:47 +08:00
|
|
|
#include "clang/AST/ASTDiagnostic.h"
|
2010-08-25 15:42:41 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2012-06-06 16:32:04 +08:00
|
|
|
#include "clang/AST/DeclFriend.h"
|
2008-08-11 13:35:13 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2008-08-11 12:54:23 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
2011-05-05 06:10:40 +08:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
2011-02-17 15:39:24 +08:00
|
|
|
#include "clang/AST/StmtCXX.h"
|
2014-05-11 00:31:55 +08:00
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
2011-09-21 02:13:03 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2009-08-27 06:33:56 +08:00
|
|
|
#include "clang/Basic/PartialDiagnostic.h"
|
2009-04-30 14:18:40 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "clang/Sema/CXXFieldCollector.h"
|
|
|
|
#include "clang/Sema/DelayedDiagnostic.h"
|
|
|
|
#include "clang/Sema/ExternalSemaSource.h"
|
|
|
|
#include "clang/Sema/MultiplexExternalSemaSource.h"
|
|
|
|
#include "clang/Sema/ObjCMethodList.h"
|
|
|
|
#include "clang/Sema/PrettyDeclStackTrace.h"
|
|
|
|
#include "clang/Sema/Scope.h"
|
|
|
|
#include "clang/Sema/ScopeInfo.h"
|
|
|
|
#include "clang/Sema/SemaConsumer.h"
|
|
|
|
#include "clang/Sema/TemplateDeduction.h"
|
|
|
|
#include "llvm/ADT/APFloat.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/SmallSet.h"
|
|
|
|
#include "llvm/Support/CrashRecoveryContext.h"
|
2006-08-18 13:17:52 +08:00
|
|
|
using namespace clang;
|
2010-08-25 16:40:02 +08:00
|
|
|
using namespace sema;
|
Keep an explicit stack of function and block scopes, each element of
which has the label map, switch statement stack, etc. Previously, we
had a single set of maps in Sema (for the function) along with a stack
of block scopes. However, this lead to funky behavior with nested
functions, e.g., in the member functions of local classes.
The explicit-stack approach is far cleaner, and we retain a 1-element
cache so that we're not malloc/free'ing every time we enter a
function. Fixes PR6382.
Also, tweaked the unused-variable warning suppression logic to look at
errors within a given Scope rather than within a given function. The
prior code wasn't looking at the right number-of-errors count when
dealing with blocks, since the block's count would be deallocated
before we got to ActOnPopScope. This approach works with nested
blocks/functions, and gives tighter error recovery.
llvm-svn: 97518
2010-03-02 07:15:13 +08:00
|
|
|
|
2014-05-03 11:45:55 +08:00
|
|
|
SourceLocation Sema::getLocForEndOfToken(SourceLocation Loc, unsigned Offset) {
|
|
|
|
return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, LangOpts);
|
|
|
|
}
|
|
|
|
|
|
|
|
ModuleLoader &Sema::getModuleLoader() const { return PP.getModuleLoader(); }
|
|
|
|
|
2012-01-17 10:15:51 +08:00
|
|
|
PrintingPolicy Sema::getPrintingPolicy(const ASTContext &Context,
|
|
|
|
const Preprocessor &PP) {
|
2011-09-28 07:30:47 +08:00
|
|
|
PrintingPolicy Policy = Context.getPrintingPolicy();
|
2012-03-11 15:00:24 +08:00
|
|
|
Policy.Bool = Context.getLangOpts().Bool;
|
2011-09-28 07:30:47 +08:00
|
|
|
if (!Policy.Bool) {
|
2013-02-20 08:54:57 +08:00
|
|
|
if (const MacroInfo *
|
|
|
|
BoolMacro = PP.getMacroInfo(&Context.Idents.get("bool"))) {
|
2013-01-20 09:04:14 +08:00
|
|
|
Policy.Bool = BoolMacro->isObjectLike() &&
|
2011-09-28 07:30:47 +08:00
|
|
|
BoolMacro->getNumTokens() == 1 &&
|
|
|
|
BoolMacro->getReplacementToken(0).is(tok::kw__Bool);
|
|
|
|
}
|
|
|
|
}
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2011-09-28 07:30:47 +08:00
|
|
|
return Policy;
|
|
|
|
}
|
|
|
|
|
2010-08-26 02:07:12 +08:00
|
|
|
void Sema::ActOnTranslationUnitScope(Scope *S) {
|
2007-11-01 02:42:27 +08:00
|
|
|
TUScope = S;
|
2008-12-12 00:49:14 +08:00
|
|
|
PushDeclContext(S, Context.getTranslationUnitDecl());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-05-29 02:45:08 +08:00
|
|
|
VAListTagName = PP.getIdentifierInfo("__va_list_tag");
|
2007-10-17 04:40:23 +08:00
|
|
|
}
|
2007-10-11 05:53:07 +08:00
|
|
|
|
2009-04-15 00:27:31 +08:00
|
|
|
Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
|
2011-08-26 06:30:56 +08:00
|
|
|
TranslationUnitKind TUKind,
|
2009-11-13 16:58:20 +08:00
|
|
|
CodeCompleteConsumer *CodeCompleter)
|
2014-01-10 06:48:32 +08:00
|
|
|
: ExternalSource(0),
|
2012-10-19 03:05:02 +08:00
|
|
|
isMultiplexExternalSource(false), FPFeatures(pp.getLangOpts()),
|
2012-03-11 15:00:24 +08:00
|
|
|
LangOpts(pp.getLangOpts()), PP(pp), Context(ctxt), Consumer(consumer),
|
2009-09-09 23:08:12 +08:00
|
|
|
Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
|
2012-10-19 03:05:02 +08:00
|
|
|
CollectStats(false), CodeCompleter(CodeCompleter),
|
2011-10-07 07:23:20 +08:00
|
|
|
CurContext(0), OriginalLexicalContext(0),
|
2014-02-11 03:50:15 +08:00
|
|
|
PackContext(0), MSStructPragmaOn(false),
|
2014-02-12 05:05:00 +08:00
|
|
|
MSPointerToMemberRepresentationMethod(
|
2014-02-13 07:50:26 +08:00
|
|
|
LangOpts.getMSPointerToMemberRepresentationMethod()),
|
|
|
|
VtorDispModeStack(1, MSVtorDispAttr::Mode(LangOpts.VtorDispMode)),
|
2014-04-09 06:30:47 +08:00
|
|
|
DataSegStack(nullptr), BSSSegStack(nullptr), ConstSegStack(nullptr),
|
|
|
|
CodeSegStack(nullptr), VisContext(0),
|
2012-09-25 12:46:05 +08:00
|
|
|
IsBuildingRecoveryCallExpr(false),
|
2011-11-10 13:35:25 +08:00
|
|
|
ExprNeedsCleanups(false), LateTemplateParser(0), OpaqueParser(0),
|
2012-01-18 06:49:33 +08:00
|
|
|
IdResolver(pp), StdInitializerList(0), CXXTypeInfoDecl(0), MSVCGuidDecl(0),
|
2012-04-23 04:43:35 +08:00
|
|
|
NSNumberDecl(0),
|
|
|
|
NSStringDecl(0), StringWithUTF8StringMethod(0),
|
|
|
|
NSArrayDecl(0), ArrayWithObjectsMethod(0),
|
2012-03-07 04:05:56 +08:00
|
|
|
NSDictionaryDecl(0), DictionaryWithObjectsMethod(0),
|
2013-01-20 09:04:14 +08:00
|
|
|
GlobalNewDeleteDeclared(false),
|
2011-08-26 06:30:56 +08:00
|
|
|
TUKind(TUKind),
|
2014-02-25 04:45:14 +08:00
|
|
|
NumSFINAEErrors(0),
|
2011-01-28 06:31:44 +08:00
|
|
|
AccessCheckingSFINAE(false), InNonInstantiationSFINAEContext(false),
|
2010-12-21 06:05:00 +08:00
|
|
|
NonInstantiationEntries(0), ArgumentPackSubstitutionIndex(-1),
|
2013-09-13 07:28:08 +08:00
|
|
|
CurrentInstantiationScope(0), DisableTypoCorrection(false),
|
|
|
|
TyposCorrected(0), AnalysisWarnings(*this),
|
|
|
|
VarDataSharingAttributesStack(0), CurScope(0),
|
2013-09-07 02:03:48 +08:00
|
|
|
Ident_super(0), Ident___float128(0)
|
2009-11-12 05:54:23 +08:00
|
|
|
{
|
2007-10-11 05:53:07 +08:00
|
|
|
TUScope = 0;
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2011-06-29 00:20:02 +08:00
|
|
|
LoadedExternalKnownNamespaces = false;
|
2012-03-07 04:05:56 +08:00
|
|
|
for (unsigned I = 0; I != NSAPI::NumNSNumberLiteralMethods; ++I)
|
|
|
|
NSNumberLiteralMethods[I] = 0;
|
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().ObjC1)
|
2012-03-07 04:05:56 +08:00
|
|
|
NSAPIObj.reset(new NSAPI(Context));
|
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().CPlusPlus)
|
2008-07-01 18:37:29 +08:00
|
|
|
FieldCollector.reset(new CXXFieldCollector());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-23 17:13:29 +08:00
|
|
|
// Tell diagnostics how to render things from the AST library.
|
2013-01-20 09:04:14 +08:00
|
|
|
PP.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
|
2010-02-10 06:26:47 +08:00
|
|
|
&Context);
|
2009-11-26 08:44:06 +08:00
|
|
|
|
|
|
|
ExprEvalContexts.push_back(
|
2012-02-22 10:04:18 +08:00
|
|
|
ExpressionEvaluationContextRecord(PotentiallyEvaluated, 0,
|
|
|
|
false, 0, false));
|
2010-08-25 16:40:02 +08:00
|
|
|
|
2010-11-19 08:19:15 +08:00
|
|
|
FunctionScopes.push_back(new FunctionScopeInfo(Diags));
|
2013-09-07 02:03:48 +08:00
|
|
|
|
|
|
|
// Initilization of data sharing attributes stack for OpenMP
|
|
|
|
InitDataSharingAttributesStack();
|
2010-08-13 06:51:45 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 23:29:05 +08:00
|
|
|
void Sema::addImplicitTypedef(StringRef Name, QualType T) {
|
|
|
|
DeclarationName DN = &Context.Idents.get(Name);
|
|
|
|
if (IdResolver.begin(DN) == IdResolver.end())
|
|
|
|
PushOnScopeChains(Context.buildImplicitTypedef(T, Name), TUScope);
|
|
|
|
}
|
|
|
|
|
2010-08-13 06:51:45 +08:00
|
|
|
void Sema::Initialize() {
|
|
|
|
// Tell the AST consumer about this Sema object.
|
|
|
|
Consumer.Initialize(Context);
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2010-08-13 06:51:45 +08:00
|
|
|
// FIXME: Isn't this redundant with the initialization above?
|
|
|
|
if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer))
|
|
|
|
SC->InitializeSema(*this);
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2010-08-13 06:51:45 +08:00
|
|
|
// Tell the external Sema source about this Sema object.
|
|
|
|
if (ExternalSemaSource *ExternalSema
|
|
|
|
= dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource()))
|
|
|
|
ExternalSema->InitializeSema(*this);
|
2011-08-12 13:46:01 +08:00
|
|
|
|
2011-08-12 14:49:56 +08:00
|
|
|
// Initialize predefined 128-bit integer types, if needed.
|
2014-05-03 11:45:55 +08:00
|
|
|
if (Context.getTargetInfo().hasInt128Type()) {
|
2011-08-12 14:49:56 +08:00
|
|
|
// If either of the 128-bit integer types are unavailable to name lookup,
|
|
|
|
// define them now.
|
|
|
|
DeclarationName Int128 = &Context.Idents.get("__int128_t");
|
Make the loading of information attached to an IdentifierInfo from an
AST file more lazy, so that we don't eagerly load that information for
all known identifiers each time a new AST file is loaded. The eager
reloading made some sense in the context of precompiled headers, since
very few identifiers were defined before PCH load time. With modules,
however, a huge amount of code can get parsed before we see an
@import, so laziness becomes important here.
The approach taken to make this information lazy is fairly simple:
when we load a new AST file, we mark all of the existing identifiers
as being out-of-date. Whenever we want to access information that may
come from an AST (e.g., whether the identifier has a macro definition,
or what top-level declarations have that name), we check the
out-of-date bit and, if it's set, ask the AST reader to update the
IdentifierInfo from the AST files. The update is a merge, and we now
take care to merge declarations before/after imports with declarations
from multiple imports.
The results of this optimization are fairly dramatic. On a small
application that brings in 14 non-trivial modules, this takes modules
from being > 3x slower than a "perfect" PCH file down to 30% slower
for a full rebuild. A partial rebuild (where the PCH file or modules
can be re-used) is down to 7% slower. Making the PCH file just a
little imperfect (e.g., adding two smallish modules used by a bunch of
.m files that aren't in the PCH file) tips the scales in favor of the
modules approach, with 24% faster partial rebuilds.
This is just a first step; the lazy scheme could possibly be improved
by adding versioning, so we don't search into modules we already
searched. Moreover, we'll need similar lazy schemes for all of the
other lookup data structures, such as DeclContexts.
llvm-svn: 143100
2011-10-27 17:33:13 +08:00
|
|
|
if (IdResolver.begin(Int128) == IdResolver.end())
|
2011-08-12 14:49:56 +08:00
|
|
|
PushOnScopeChains(Context.getInt128Decl(), TUScope);
|
|
|
|
|
|
|
|
DeclarationName UInt128 = &Context.Idents.get("__uint128_t");
|
Make the loading of information attached to an IdentifierInfo from an
AST file more lazy, so that we don't eagerly load that information for
all known identifiers each time a new AST file is loaded. The eager
reloading made some sense in the context of precompiled headers, since
very few identifiers were defined before PCH load time. With modules,
however, a huge amount of code can get parsed before we see an
@import, so laziness becomes important here.
The approach taken to make this information lazy is fairly simple:
when we load a new AST file, we mark all of the existing identifiers
as being out-of-date. Whenever we want to access information that may
come from an AST (e.g., whether the identifier has a macro definition,
or what top-level declarations have that name), we check the
out-of-date bit and, if it's set, ask the AST reader to update the
IdentifierInfo from the AST files. The update is a merge, and we now
take care to merge declarations before/after imports with declarations
from multiple imports.
The results of this optimization are fairly dramatic. On a small
application that brings in 14 non-trivial modules, this takes modules
from being > 3x slower than a "perfect" PCH file down to 30% slower
for a full rebuild. A partial rebuild (where the PCH file or modules
can be re-used) is down to 7% slower. Making the PCH file just a
little imperfect (e.g., adding two smallish modules used by a bunch of
.m files that aren't in the PCH file) tips the scales in favor of the
modules approach, with 24% faster partial rebuilds.
This is just a first step; the lazy scheme could possibly be improved
by adding versioning, so we don't search into modules we already
searched. Moreover, we'll need similar lazy schemes for all of the
other lookup data structures, such as DeclContexts.
llvm-svn: 143100
2011-10-27 17:33:13 +08:00
|
|
|
if (IdResolver.begin(UInt128) == IdResolver.end())
|
2011-08-12 14:49:56 +08:00
|
|
|
PushOnScopeChains(Context.getUInt128Decl(), TUScope);
|
|
|
|
}
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2011-08-12 14:49:56 +08:00
|
|
|
|
2011-08-12 13:46:01 +08:00
|
|
|
// Initialize predefined Objective-C types:
|
2012-03-11 15:00:24 +08:00
|
|
|
if (PP.getLangOpts().ObjC1) {
|
2011-08-12 14:17:30 +08:00
|
|
|
// If 'SEL' does not yet refer to any declarations, make it refer to the
|
|
|
|
// predefined 'SEL'.
|
|
|
|
DeclarationName SEL = &Context.Idents.get("SEL");
|
Make the loading of information attached to an IdentifierInfo from an
AST file more lazy, so that we don't eagerly load that information for
all known identifiers each time a new AST file is loaded. The eager
reloading made some sense in the context of precompiled headers, since
very few identifiers were defined before PCH load time. With modules,
however, a huge amount of code can get parsed before we see an
@import, so laziness becomes important here.
The approach taken to make this information lazy is fairly simple:
when we load a new AST file, we mark all of the existing identifiers
as being out-of-date. Whenever we want to access information that may
come from an AST (e.g., whether the identifier has a macro definition,
or what top-level declarations have that name), we check the
out-of-date bit and, if it's set, ask the AST reader to update the
IdentifierInfo from the AST files. The update is a merge, and we now
take care to merge declarations before/after imports with declarations
from multiple imports.
The results of this optimization are fairly dramatic. On a small
application that brings in 14 non-trivial modules, this takes modules
from being > 3x slower than a "perfect" PCH file down to 30% slower
for a full rebuild. A partial rebuild (where the PCH file or modules
can be re-used) is down to 7% slower. Making the PCH file just a
little imperfect (e.g., adding two smallish modules used by a bunch of
.m files that aren't in the PCH file) tips the scales in favor of the
modules approach, with 24% faster partial rebuilds.
This is just a first step; the lazy scheme could possibly be improved
by adding versioning, so we don't search into modules we already
searched. Moreover, we'll need similar lazy schemes for all of the
other lookup data structures, such as DeclContexts.
llvm-svn: 143100
2011-10-27 17:33:13 +08:00
|
|
|
if (IdResolver.begin(SEL) == IdResolver.end())
|
2011-08-12 14:17:30 +08:00
|
|
|
PushOnScopeChains(Context.getObjCSelDecl(), TUScope);
|
|
|
|
|
2011-08-12 13:46:01 +08:00
|
|
|
// If 'id' does not yet refer to any declarations, make it refer to the
|
|
|
|
// predefined 'id'.
|
|
|
|
DeclarationName Id = &Context.Idents.get("id");
|
Make the loading of information attached to an IdentifierInfo from an
AST file more lazy, so that we don't eagerly load that information for
all known identifiers each time a new AST file is loaded. The eager
reloading made some sense in the context of precompiled headers, since
very few identifiers were defined before PCH load time. With modules,
however, a huge amount of code can get parsed before we see an
@import, so laziness becomes important here.
The approach taken to make this information lazy is fairly simple:
when we load a new AST file, we mark all of the existing identifiers
as being out-of-date. Whenever we want to access information that may
come from an AST (e.g., whether the identifier has a macro definition,
or what top-level declarations have that name), we check the
out-of-date bit and, if it's set, ask the AST reader to update the
IdentifierInfo from the AST files. The update is a merge, and we now
take care to merge declarations before/after imports with declarations
from multiple imports.
The results of this optimization are fairly dramatic. On a small
application that brings in 14 non-trivial modules, this takes modules
from being > 3x slower than a "perfect" PCH file down to 30% slower
for a full rebuild. A partial rebuild (where the PCH file or modules
can be re-used) is down to 7% slower. Making the PCH file just a
little imperfect (e.g., adding two smallish modules used by a bunch of
.m files that aren't in the PCH file) tips the scales in favor of the
modules approach, with 24% faster partial rebuilds.
This is just a first step; the lazy scheme could possibly be improved
by adding versioning, so we don't search into modules we already
searched. Moreover, we'll need similar lazy schemes for all of the
other lookup data structures, such as DeclContexts.
llvm-svn: 143100
2011-10-27 17:33:13 +08:00
|
|
|
if (IdResolver.begin(Id) == IdResolver.end())
|
2011-08-12 13:46:01 +08:00
|
|
|
PushOnScopeChains(Context.getObjCIdDecl(), TUScope);
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2011-08-12 13:59:41 +08:00
|
|
|
// Create the built-in typedef for 'Class'.
|
|
|
|
DeclarationName Class = &Context.Idents.get("Class");
|
Make the loading of information attached to an IdentifierInfo from an
AST file more lazy, so that we don't eagerly load that information for
all known identifiers each time a new AST file is loaded. The eager
reloading made some sense in the context of precompiled headers, since
very few identifiers were defined before PCH load time. With modules,
however, a huge amount of code can get parsed before we see an
@import, so laziness becomes important here.
The approach taken to make this information lazy is fairly simple:
when we load a new AST file, we mark all of the existing identifiers
as being out-of-date. Whenever we want to access information that may
come from an AST (e.g., whether the identifier has a macro definition,
or what top-level declarations have that name), we check the
out-of-date bit and, if it's set, ask the AST reader to update the
IdentifierInfo from the AST files. The update is a merge, and we now
take care to merge declarations before/after imports with declarations
from multiple imports.
The results of this optimization are fairly dramatic. On a small
application that brings in 14 non-trivial modules, this takes modules
from being > 3x slower than a "perfect" PCH file down to 30% slower
for a full rebuild. A partial rebuild (where the PCH file or modules
can be re-used) is down to 7% slower. Making the PCH file just a
little imperfect (e.g., adding two smallish modules used by a bunch of
.m files that aren't in the PCH file) tips the scales in favor of the
modules approach, with 24% faster partial rebuilds.
This is just a first step; the lazy scheme could possibly be improved
by adding versioning, so we don't search into modules we already
searched. Moreover, we'll need similar lazy schemes for all of the
other lookup data structures, such as DeclContexts.
llvm-svn: 143100
2011-10-27 17:33:13 +08:00
|
|
|
if (IdResolver.begin(Class) == IdResolver.end())
|
2011-08-12 13:59:41 +08:00
|
|
|
PushOnScopeChains(Context.getObjCClassDecl(), TUScope);
|
2012-01-18 02:09:05 +08:00
|
|
|
|
|
|
|
// Create the built-in forward declaratino for 'Protocol'.
|
|
|
|
DeclarationName Protocol = &Context.Idents.get("Protocol");
|
|
|
|
if (IdResolver.begin(Protocol) == IdResolver.end())
|
|
|
|
PushOnScopeChains(Context.getObjCProtocolDecl(), TUScope);
|
2011-08-12 13:46:01 +08:00
|
|
|
}
|
2012-06-16 11:34:49 +08:00
|
|
|
|
2014-01-04 23:25:02 +08:00
|
|
|
// Initialize Microsoft "predefined C++ types".
|
2014-01-14 20:51:41 +08:00
|
|
|
if (PP.getLangOpts().MSVCCompat && PP.getLangOpts().CPlusPlus) {
|
2014-01-04 23:25:02 +08:00
|
|
|
if (IdResolver.begin(&Context.Idents.get("type_info")) == IdResolver.end())
|
|
|
|
PushOnScopeChains(Context.buildImplicitRecord("type_info", TTK_Class),
|
|
|
|
TUScope);
|
2014-01-14 14:19:35 +08:00
|
|
|
|
|
|
|
addImplicitTypedef("size_t", Context.getSizeType());
|
2014-01-04 23:25:02 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 23:29:05 +08:00
|
|
|
// Initialize predefined OpenCL types.
|
|
|
|
if (PP.getLangOpts().OpenCL) {
|
|
|
|
addImplicitTypedef("image1d_t", Context.OCLImage1dTy);
|
|
|
|
addImplicitTypedef("image1d_array_t", Context.OCLImage1dArrayTy);
|
|
|
|
addImplicitTypedef("image1d_buffer_t", Context.OCLImage1dBufferTy);
|
|
|
|
addImplicitTypedef("image2d_t", Context.OCLImage2dTy);
|
|
|
|
addImplicitTypedef("image2d_array_t", Context.OCLImage2dArrayTy);
|
|
|
|
addImplicitTypedef("image3d_t", Context.OCLImage3dTy);
|
|
|
|
addImplicitTypedef("sampler_t", Context.OCLSamplerTy);
|
|
|
|
addImplicitTypedef("event_t", Context.OCLEventTy);
|
|
|
|
}
|
|
|
|
|
2012-06-16 11:34:49 +08:00
|
|
|
DeclarationName BuiltinVaList = &Context.Idents.get("__builtin_va_list");
|
|
|
|
if (IdResolver.begin(BuiltinVaList) == IdResolver.end())
|
|
|
|
PushOnScopeChains(Context.getBuiltinVaListDecl(), TUScope);
|
2007-02-28 09:22:02 +08:00
|
|
|
}
|
2006-11-10 14:20:45 +08:00
|
|
|
|
2010-01-10 20:58:08 +08:00
|
|
|
Sema::~Sema() {
|
2014-02-20 07:44:52 +08:00
|
|
|
llvm::DeleteContainerSeconds(LateParsedTemplateMap);
|
2010-01-10 20:58:08 +08:00
|
|
|
if (PackContext) FreePackedContext();
|
2010-08-05 14:57:20 +08:00
|
|
|
if (VisContext) FreeVisContext();
|
2010-08-25 16:40:02 +08:00
|
|
|
// Kill all the active scopes.
|
|
|
|
for (unsigned I = 1, E = FunctionScopes.size(); I != E; ++I)
|
|
|
|
delete FunctionScopes[I];
|
|
|
|
if (FunctionScopes.size() == 1)
|
|
|
|
delete FunctionScopes[0];
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2010-08-13 06:51:45 +08:00
|
|
|
// Tell the SemaConsumer to forget about us; we're going out of scope.
|
|
|
|
if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer))
|
|
|
|
SC->ForgetSema();
|
|
|
|
|
|
|
|
// Detach from the external Sema source.
|
|
|
|
if (ExternalSemaSource *ExternalSema
|
2010-08-13 11:15:25 +08:00
|
|
|
= dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource()))
|
2010-08-13 06:51:45 +08:00
|
|
|
ExternalSema->ForgetSema();
|
2012-10-19 03:05:02 +08:00
|
|
|
|
|
|
|
// If Sema's ExternalSource is the multiplexer - we own it.
|
|
|
|
if (isMultiplexExternalSource)
|
|
|
|
delete ExternalSource;
|
2013-09-07 02:03:48 +08:00
|
|
|
|
|
|
|
// Destroys data sharing attributes stack for OpenMP
|
|
|
|
DestroyDataSharingAttributesStack();
|
2010-01-10 20:58:08 +08:00
|
|
|
}
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
/// makeUnavailableInSystemHeader - There is an error in the current
|
|
|
|
/// context. If we're still in a system header, and we can plausibly
|
|
|
|
/// make the relevant declaration unavailable instead of erroring, do
|
|
|
|
/// so and return true.
|
|
|
|
bool Sema::makeUnavailableInSystemHeader(SourceLocation loc,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef msg) {
|
2011-06-16 07:02:42 +08:00
|
|
|
// If we're not in a function, it's an error.
|
|
|
|
FunctionDecl *fn = dyn_cast<FunctionDecl>(CurContext);
|
|
|
|
if (!fn) return false;
|
|
|
|
|
|
|
|
// If we're in template instantiation, it's an error.
|
|
|
|
if (!ActiveTemplateInstantiations.empty())
|
|
|
|
return false;
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
// If that function's not in a system header, it's an error.
|
|
|
|
if (!Context.getSourceManager().isInSystemHeader(loc))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If the function is already unavailable, it's not an error.
|
|
|
|
if (fn->hasAttr<UnavailableAttr>()) return true;
|
|
|
|
|
2014-01-16 21:03:14 +08:00
|
|
|
fn->addAttr(UnavailableAttr::CreateImplicit(Context, msg, loc));
|
2011-06-16 07:02:42 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-04-25 00:28:06 +08:00
|
|
|
ASTMutationListener *Sema::getASTMutationListener() const {
|
|
|
|
return getASTConsumer().GetASTMutationListener();
|
|
|
|
}
|
|
|
|
|
2012-10-19 03:05:02 +08:00
|
|
|
///\brief Registers an external source. If an external source already exists,
|
|
|
|
/// creates a multiplex external source and appends to it.
|
|
|
|
///
|
|
|
|
///\param[in] E - A non-null external sema source.
|
|
|
|
///
|
|
|
|
void Sema::addExternalSource(ExternalSemaSource *E) {
|
|
|
|
assert(E && "Cannot use with NULL ptr");
|
|
|
|
|
|
|
|
if (!ExternalSource) {
|
|
|
|
ExternalSource = E;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isMultiplexExternalSource)
|
|
|
|
static_cast<MultiplexExternalSemaSource*>(ExternalSource)->addSource(*E);
|
|
|
|
else {
|
|
|
|
ExternalSource = new MultiplexExternalSemaSource(*ExternalSource, *E);
|
|
|
|
isMultiplexExternalSource = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-07 00:21:37 +08:00
|
|
|
/// \brief Print out statistics about the semantic analysis.
|
|
|
|
void Sema::PrintStats() const {
|
|
|
|
llvm::errs() << "\n*** Semantic Analysis Stats:\n";
|
|
|
|
llvm::errs() << NumSFINAEErrors << " SFINAE diagnostics trapped.\n";
|
|
|
|
|
|
|
|
BumpAlloc.PrintStats();
|
|
|
|
AnalysisWarnings.PrintStats();
|
|
|
|
}
|
|
|
|
|
2011-11-30 06:48:16 +08:00
|
|
|
/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
|
|
|
|
/// If there is already an implicit cast, merge into the existing one.
|
|
|
|
/// The result is of the given category.
|
|
|
|
ExprResult Sema::ImpCastExprToType(Expr *E, QualType Ty,
|
|
|
|
CastKind Kind, ExprValueKind VK,
|
|
|
|
const CXXCastPath *BasePath,
|
|
|
|
CheckedConversionKind CCK) {
|
2011-10-28 11:31:48 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
if (VK == VK_RValue && !E->isRValue()) {
|
|
|
|
switch (Kind) {
|
|
|
|
default:
|
|
|
|
assert(0 && "can't implicitly cast lvalue to rvalue with this cast kind");
|
|
|
|
case CK_LValueToRValue:
|
|
|
|
case CK_ArrayToPointerDecay:
|
|
|
|
case CK_FunctionToPointerDecay:
|
|
|
|
case CK_ToVoid:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-11-11 07:32:36 +08:00
|
|
|
assert((VK == VK_RValue || !E->isRValue()) && "can't cast rvalue to lvalue");
|
2011-10-28 11:31:48 +08:00
|
|
|
#endif
|
|
|
|
|
2011-04-09 02:41:53 +08:00
|
|
|
QualType ExprTy = Context.getCanonicalType(E->getType());
|
2008-09-04 16:38:01 +08:00
|
|
|
QualType TypeTy = Context.getCanonicalType(Ty);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-04 16:38:01 +08:00
|
|
|
if (ExprTy == TypeTy)
|
2011-04-09 02:41:53 +08:00
|
|
|
return Owned(E);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
Rework when and how vtables are emitted, by tracking where vtables are
"used" (e.g., we will refer to the vtable in the generated code) and
when they are defined (i.e., because we've seen the key function
definition). Previously, we were effectively tracking "potential
definitions" rather than uses, so we were a bit too eager about emitting
vtables for classes without key functions.
The new scheme:
- For every use of a vtable, Sema calls MarkVTableUsed() to indicate
the use. For example, this occurs when calling a virtual member
function of the class, defining a constructor of that class type,
dynamic_cast'ing from that type to a derived class, casting
to/through a virtual base class, etc.
- For every definition of a vtable, Sema calls MarkVTableUsed() to
indicate the definition. This happens at the end of the translation
unit for classes whose key function has been defined (so we can
delay computation of the key function; see PR6564), and will also
occur with explicit template instantiation definitions.
- For every vtable defined/used, we mark all of the virtual member
functions of that vtable as defined/used, unless we know that the key
function is in another translation unit. This instantiates virtual
member functions when needed.
- At the end of the translation unit, Sema tells CodeGen (via the
ASTConsumer) which vtables must be defined (CodeGen will define
them) and which may be used (for which CodeGen will define the
vtables lazily).
From a language perspective, both the old and the new schemes are
permissible: we're allowed to instantiate virtual member functions
whenever we want per the standard. However, all other C++ compilers
were more lazy than we were, and our eagerness was both a performance
issue (we instantiated too much) and a portability problem (we broke
Boost test cases, which now pass).
Notes:
(1) There's a ton of churn in the tests, because the order in which
vtables get emitted to IR has changed. I've tried to isolate some of
the larger tests from these issues.
(2) Some diagnostics related to
implicitly-instantiated/implicitly-defined virtual member functions
have moved to the point of first use/definition. It's better this
way.
(3) I could use a review of the places where we MarkVTableUsed, to
see if I missed any place where the language effectively requires a
vtable.
Fixes PR7114 and PR6564.
llvm-svn: 103718
2010-05-14 00:44:06 +08:00
|
|
|
// If this is a derived-to-base cast to a through a virtual base, we
|
|
|
|
// need a vtable.
|
2013-01-20 09:04:14 +08:00
|
|
|
if (Kind == CK_DerivedToBase &&
|
2010-08-07 14:22:56 +08:00
|
|
|
BasePathInvolvesVirtualBase(*BasePath)) {
|
2011-04-09 02:41:53 +08:00
|
|
|
QualType T = E->getType();
|
Rework when and how vtables are emitted, by tracking where vtables are
"used" (e.g., we will refer to the vtable in the generated code) and
when they are defined (i.e., because we've seen the key function
definition). Previously, we were effectively tracking "potential
definitions" rather than uses, so we were a bit too eager about emitting
vtables for classes without key functions.
The new scheme:
- For every use of a vtable, Sema calls MarkVTableUsed() to indicate
the use. For example, this occurs when calling a virtual member
function of the class, defining a constructor of that class type,
dynamic_cast'ing from that type to a derived class, casting
to/through a virtual base class, etc.
- For every definition of a vtable, Sema calls MarkVTableUsed() to
indicate the definition. This happens at the end of the translation
unit for classes whose key function has been defined (so we can
delay computation of the key function; see PR6564), and will also
occur with explicit template instantiation definitions.
- For every vtable defined/used, we mark all of the virtual member
functions of that vtable as defined/used, unless we know that the key
function is in another translation unit. This instantiates virtual
member functions when needed.
- At the end of the translation unit, Sema tells CodeGen (via the
ASTConsumer) which vtables must be defined (CodeGen will define
them) and which may be used (for which CodeGen will define the
vtables lazily).
From a language perspective, both the old and the new schemes are
permissible: we're allowed to instantiate virtual member functions
whenever we want per the standard. However, all other C++ compilers
were more lazy than we were, and our eagerness was both a performance
issue (we instantiated too much) and a portability problem (we broke
Boost test cases, which now pass).
Notes:
(1) There's a ton of churn in the tests, because the order in which
vtables get emitted to IR has changed. I've tried to isolate some of
the larger tests from these issues.
(2) Some diagnostics related to
implicitly-instantiated/implicitly-defined virtual member functions
have moved to the point of first use/definition. It's better this
way.
(3) I could use a review of the places where we MarkVTableUsed, to
see if I missed any place where the language effectively requires a
vtable.
Fixes PR7114 and PR6564.
llvm-svn: 103718
2010-05-14 00:44:06 +08:00
|
|
|
if (const PointerType *Pointer = T->getAs<PointerType>())
|
|
|
|
T = Pointer->getPointeeType();
|
|
|
|
if (const RecordType *RecordTy = T->getAs<RecordType>())
|
2013-01-20 09:04:14 +08:00
|
|
|
MarkVTableUsed(E->getLocStart(),
|
Rework when and how vtables are emitted, by tracking where vtables are
"used" (e.g., we will refer to the vtable in the generated code) and
when they are defined (i.e., because we've seen the key function
definition). Previously, we were effectively tracking "potential
definitions" rather than uses, so we were a bit too eager about emitting
vtables for classes without key functions.
The new scheme:
- For every use of a vtable, Sema calls MarkVTableUsed() to indicate
the use. For example, this occurs when calling a virtual member
function of the class, defining a constructor of that class type,
dynamic_cast'ing from that type to a derived class, casting
to/through a virtual base class, etc.
- For every definition of a vtable, Sema calls MarkVTableUsed() to
indicate the definition. This happens at the end of the translation
unit for classes whose key function has been defined (so we can
delay computation of the key function; see PR6564), and will also
occur with explicit template instantiation definitions.
- For every vtable defined/used, we mark all of the virtual member
functions of that vtable as defined/used, unless we know that the key
function is in another translation unit. This instantiates virtual
member functions when needed.
- At the end of the translation unit, Sema tells CodeGen (via the
ASTConsumer) which vtables must be defined (CodeGen will define
them) and which may be used (for which CodeGen will define the
vtables lazily).
From a language perspective, both the old and the new schemes are
permissible: we're allowed to instantiate virtual member functions
whenever we want per the standard. However, all other C++ compilers
were more lazy than we were, and our eagerness was both a performance
issue (we instantiated too much) and a portability problem (we broke
Boost test cases, which now pass).
Notes:
(1) There's a ton of churn in the tests, because the order in which
vtables get emitted to IR has changed. I've tried to isolate some of
the larger tests from these issues.
(2) Some diagnostics related to
implicitly-instantiated/implicitly-defined virtual member functions
have moved to the point of first use/definition. It's better this
way.
(3) I could use a review of the places where we MarkVTableUsed, to
see if I missed any place where the language effectively requires a
vtable.
Fixes PR7114 and PR6564.
llvm-svn: 103718
2010-05-14 00:44:06 +08:00
|
|
|
cast<CXXRecordDecl>(RecordTy->getDecl()));
|
|
|
|
}
|
2011-11-30 06:48:16 +08:00
|
|
|
|
|
|
|
if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(E)) {
|
|
|
|
if (ImpCast->getCastKind() == Kind && (!BasePath || BasePath->empty())) {
|
|
|
|
ImpCast->setType(Ty);
|
|
|
|
ImpCast->setValueKind(VK);
|
|
|
|
return Owned(E);
|
|
|
|
}
|
2009-09-15 13:28:24 +08:00
|
|
|
}
|
|
|
|
|
2011-11-30 06:48:16 +08:00
|
|
|
return Owned(ImplicitCastExpr::Create(Context, Ty, Kind, E, BasePath, VK));
|
2010-07-20 12:20:21 +08:00
|
|
|
}
|
|
|
|
|
2011-04-07 17:26:19 +08:00
|
|
|
/// ScalarTypeToBooleanCastKind - Returns the cast kind corresponding
|
|
|
|
/// to the conversion from scalar type ScalarTy to the Boolean type.
|
|
|
|
CastKind Sema::ScalarTypeToBooleanCastKind(QualType ScalarTy) {
|
|
|
|
switch (ScalarTy->getScalarTypeKind()) {
|
|
|
|
case Type::STK_Bool: return CK_NoOp;
|
2011-09-09 13:25:32 +08:00
|
|
|
case Type::STK_CPointer: return CK_PointerToBoolean;
|
|
|
|
case Type::STK_BlockPointer: return CK_PointerToBoolean;
|
|
|
|
case Type::STK_ObjCObjectPointer: return CK_PointerToBoolean;
|
2011-04-07 17:26:19 +08:00
|
|
|
case Type::STK_MemberPointer: return CK_MemberPointerToBoolean;
|
|
|
|
case Type::STK_Integral: return CK_IntegralToBoolean;
|
|
|
|
case Type::STK_Floating: return CK_FloatingToBoolean;
|
|
|
|
case Type::STK_IntegralComplex: return CK_IntegralComplexToBoolean;
|
|
|
|
case Type::STK_FloatingComplex: return CK_FloatingComplexToBoolean;
|
|
|
|
}
|
|
|
|
return CK_Invalid;
|
|
|
|
}
|
|
|
|
|
2010-08-15 09:15:20 +08:00
|
|
|
/// \brief Used to prune the decls of Sema's UnusedFileScopedDecls vector.
|
|
|
|
static bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) {
|
2013-01-09 03:43:34 +08:00
|
|
|
if (D->getMostRecentDecl()->isUsed())
|
2010-08-15 09:15:20 +08:00
|
|
|
return true;
|
|
|
|
|
2013-05-13 08:12:11 +08:00
|
|
|
if (D->isExternallyVisible())
|
2013-03-14 11:07:35 +08:00
|
|
|
return true;
|
|
|
|
|
2010-08-15 09:15:20 +08:00
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
// UnusedFileScopedDecls stores the first declaration.
|
|
|
|
// The declaration may have become definition so check again.
|
|
|
|
const FunctionDecl *DeclToCheck;
|
|
|
|
if (FD->hasBody(DeclToCheck))
|
|
|
|
return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
|
|
|
|
|
|
|
|
// Later redecls may add new information resulting in not having to warn,
|
|
|
|
// so check again.
|
2012-01-15 00:38:05 +08:00
|
|
|
DeclToCheck = FD->getMostRecentDecl();
|
2010-08-15 09:15:20 +08:00
|
|
|
if (DeclToCheck != FD)
|
|
|
|
return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
|
2013-09-11 05:10:25 +08:00
|
|
|
// If a variable usable in constant expressions is referenced,
|
|
|
|
// don't warn if it isn't used: if the value of a variable is required
|
|
|
|
// for the computation of a constant expression, it doesn't make sense to
|
|
|
|
// warn even if the variable isn't odr-used. (isReferenced doesn't
|
|
|
|
// precisely reflect that, but it's a decent approximation.)
|
|
|
|
if (VD->isReferenced() &&
|
|
|
|
VD->isUsableInConstantExpressions(SemaRef->Context))
|
|
|
|
return true;
|
|
|
|
|
2010-08-15 09:15:20 +08:00
|
|
|
// UnusedFileScopedDecls stores the first declaration.
|
|
|
|
// The declaration may have become definition so check again.
|
2013-01-20 09:04:14 +08:00
|
|
|
const VarDecl *DeclToCheck = VD->getDefinition();
|
2010-08-15 09:15:20 +08:00
|
|
|
if (DeclToCheck)
|
|
|
|
return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
|
|
|
|
|
|
|
|
// Later redecls may add new information resulting in not having to warn,
|
|
|
|
// so check again.
|
2012-01-15 00:38:05 +08:00
|
|
|
DeclToCheck = VD->getMostRecentDecl();
|
2010-08-15 09:15:20 +08:00
|
|
|
if (DeclToCheck != VD)
|
|
|
|
return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-31 11:23:57 +08:00
|
|
|
/// Obtains a sorted list of functions that are undefined but ODR-used.
|
2013-02-01 16:13:20 +08:00
|
|
|
void Sema::getUndefinedButUsed(
|
2013-01-31 11:23:57 +08:00
|
|
|
SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> > &Undefined) {
|
|
|
|
for (llvm::DenseMap<NamedDecl *, SourceLocation>::iterator
|
2013-02-01 16:13:20 +08:00
|
|
|
I = UndefinedButUsed.begin(), E = UndefinedButUsed.end();
|
2013-01-31 11:23:57 +08:00
|
|
|
I != E; ++I) {
|
|
|
|
NamedDecl *ND = I->first;
|
2011-02-19 10:53:41 +08:00
|
|
|
|
|
|
|
// Ignore attributes that have become invalid.
|
2013-01-31 11:23:57 +08:00
|
|
|
if (ND->isInvalidDecl()) continue;
|
2011-02-19 10:53:41 +08:00
|
|
|
|
|
|
|
// __attribute__((weakref)) is basically a definition.
|
2013-01-31 11:23:57 +08:00
|
|
|
if (ND->hasAttr<WeakRefAttr>()) continue;
|
2011-02-19 10:53:41 +08:00
|
|
|
|
2013-01-31 11:23:57 +08:00
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
|
|
|
|
if (FD->isDefined())
|
2011-02-19 10:53:41 +08:00
|
|
|
continue;
|
2013-05-13 08:12:11 +08:00
|
|
|
if (FD->isExternallyVisible() &&
|
2013-02-01 16:13:20 +08:00
|
|
|
!FD->getMostRecentDecl()->isInlined())
|
|
|
|
continue;
|
2011-02-19 10:53:41 +08:00
|
|
|
} else {
|
2013-01-31 11:23:57 +08:00
|
|
|
if (cast<VarDecl>(ND)->hasDefinition() != VarDecl::DeclarationOnly)
|
2011-02-19 10:53:41 +08:00
|
|
|
continue;
|
2013-05-13 08:12:11 +08:00
|
|
|
if (ND->isExternallyVisible())
|
2013-02-01 16:13:20 +08:00
|
|
|
continue;
|
2011-02-19 10:53:41 +08:00
|
|
|
}
|
|
|
|
|
2013-01-31 11:23:57 +08:00
|
|
|
Undefined.push_back(std::make_pair(ND, I->second));
|
|
|
|
}
|
|
|
|
|
2013-02-01 16:13:20 +08:00
|
|
|
// Sort (in order of use site) so that we're not dependent on the iteration
|
|
|
|
// order through an llvm::DenseMap.
|
2014-03-01 22:48:57 +08:00
|
|
|
SourceManager &SM = Context.getSourceManager();
|
2013-01-31 11:23:57 +08:00
|
|
|
std::sort(Undefined.begin(), Undefined.end(),
|
2014-03-01 22:48:57 +08:00
|
|
|
[&SM](const std::pair<NamedDecl *, SourceLocation> &l,
|
|
|
|
const std::pair<NamedDecl *, SourceLocation> &r) {
|
|
|
|
if (l.second.isValid() && !r.second.isValid())
|
|
|
|
return true;
|
|
|
|
if (!l.second.isValid() && r.second.isValid())
|
|
|
|
return false;
|
|
|
|
if (l.second != r.second)
|
|
|
|
return SM.isBeforeInTranslationUnit(l.second, r.second);
|
|
|
|
return SM.isBeforeInTranslationUnit(l.first->getLocation(),
|
|
|
|
r.first->getLocation());
|
|
|
|
});
|
2013-01-31 11:23:57 +08:00
|
|
|
}
|
|
|
|
|
2013-02-01 16:13:20 +08:00
|
|
|
/// checkUndefinedButUsed - Check for undefined objects with internal linkage
|
|
|
|
/// or that are inline.
|
|
|
|
static void checkUndefinedButUsed(Sema &S) {
|
|
|
|
if (S.UndefinedButUsed.empty()) return;
|
2013-01-31 11:23:57 +08:00
|
|
|
|
|
|
|
// Collect all the still-undefined entities with internal linkage.
|
|
|
|
SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
|
2013-02-01 16:13:20 +08:00
|
|
|
S.getUndefinedButUsed(Undefined);
|
2013-01-31 11:23:57 +08:00
|
|
|
if (Undefined.empty()) return;
|
|
|
|
|
|
|
|
for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
|
|
|
|
I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
|
|
|
|
NamedDecl *ND = I->first;
|
|
|
|
|
2013-05-13 08:12:11 +08:00
|
|
|
if (!ND->isExternallyVisible()) {
|
2013-02-01 16:13:20 +08:00
|
|
|
S.Diag(ND->getLocation(), diag::warn_undefined_internal)
|
|
|
|
<< isa<VarDecl>(ND) << ND;
|
|
|
|
} else {
|
|
|
|
assert(cast<FunctionDecl>(ND)->getMostRecentDecl()->isInlined() &&
|
|
|
|
"used object requires definition but isn't inline or internal?");
|
|
|
|
S.Diag(ND->getLocation(), diag::warn_undefined_inline) << ND;
|
|
|
|
}
|
|
|
|
if (I->second.isValid())
|
|
|
|
S.Diag(I->second, diag::note_used_here);
|
2011-02-19 10:53:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-29 02:09:57 +08:00
|
|
|
void Sema::LoadExternalWeakUndeclaredIdentifiers() {
|
|
|
|
if (!ExternalSource)
|
|
|
|
return;
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2011-07-29 02:09:57 +08:00
|
|
|
SmallVector<std::pair<IdentifierInfo *, WeakInfo>, 4> WeakIDs;
|
|
|
|
ExternalSource->ReadWeakUndeclaredIdentifiers(WeakIDs);
|
|
|
|
for (unsigned I = 0, N = WeakIDs.size(); I != N; ++I) {
|
|
|
|
llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator Pos
|
|
|
|
= WeakUndeclaredIdentifiers.find(WeakIDs[I].first);
|
|
|
|
if (Pos != WeakUndeclaredIdentifiers.end())
|
|
|
|
continue;
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2011-07-29 02:09:57 +08:00
|
|
|
WeakUndeclaredIdentifiers.insert(WeakIDs[I]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-06 16:32:04 +08:00
|
|
|
|
|
|
|
typedef llvm::DenseMap<const CXXRecordDecl*, bool> RecordCompleteMap;
|
|
|
|
|
|
|
|
/// \brief Returns true, if all methods and nested classes of the given
|
|
|
|
/// CXXRecordDecl are defined in this translation unit.
|
|
|
|
///
|
|
|
|
/// Should only be called from ActOnEndOfTranslationUnit so that all
|
|
|
|
/// definitions are actually read.
|
|
|
|
static bool MethodsAndNestedClassesComplete(const CXXRecordDecl *RD,
|
|
|
|
RecordCompleteMap &MNCComplete) {
|
|
|
|
RecordCompleteMap::iterator Cache = MNCComplete.find(RD);
|
|
|
|
if (Cache != MNCComplete.end())
|
|
|
|
return Cache->second;
|
|
|
|
if (!RD->isCompleteDefinition())
|
|
|
|
return false;
|
|
|
|
bool Complete = true;
|
|
|
|
for (DeclContext::decl_iterator I = RD->decls_begin(),
|
|
|
|
E = RD->decls_end();
|
|
|
|
I != E && Complete; ++I) {
|
|
|
|
if (const CXXMethodDecl *M = dyn_cast<CXXMethodDecl>(*I))
|
|
|
|
Complete = M->isDefined() || (M->isPure() && !isa<CXXDestructorDecl>(M));
|
2012-06-15 04:56:06 +08:00
|
|
|
else if (const FunctionTemplateDecl *F = dyn_cast<FunctionTemplateDecl>(*I))
|
|
|
|
Complete = F->getTemplatedDecl()->isDefined();
|
2012-06-06 16:32:04 +08:00
|
|
|
else if (const CXXRecordDecl *R = dyn_cast<CXXRecordDecl>(*I)) {
|
|
|
|
if (R->isInjectedClassName())
|
|
|
|
continue;
|
|
|
|
if (R->hasDefinition())
|
|
|
|
Complete = MethodsAndNestedClassesComplete(R->getDefinition(),
|
|
|
|
MNCComplete);
|
|
|
|
else
|
|
|
|
Complete = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MNCComplete[RD] = Complete;
|
|
|
|
return Complete;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Returns true, if the given CXXRecordDecl is fully defined in this
|
|
|
|
/// translation unit, i.e. all methods are defined or pure virtual and all
|
|
|
|
/// friends, friend functions and nested classes are fully defined in this
|
|
|
|
/// translation unit.
|
|
|
|
///
|
|
|
|
/// Should only be called from ActOnEndOfTranslationUnit so that all
|
|
|
|
/// definitions are actually read.
|
|
|
|
static bool IsRecordFullyDefined(const CXXRecordDecl *RD,
|
|
|
|
RecordCompleteMap &RecordsComplete,
|
|
|
|
RecordCompleteMap &MNCComplete) {
|
|
|
|
RecordCompleteMap::iterator Cache = RecordsComplete.find(RD);
|
|
|
|
if (Cache != RecordsComplete.end())
|
|
|
|
return Cache->second;
|
|
|
|
bool Complete = MethodsAndNestedClassesComplete(RD, MNCComplete);
|
|
|
|
for (CXXRecordDecl::friend_iterator I = RD->friend_begin(),
|
|
|
|
E = RD->friend_end();
|
|
|
|
I != E && Complete; ++I) {
|
|
|
|
// Check if friend classes and methods are complete.
|
|
|
|
if (TypeSourceInfo *TSI = (*I)->getFriendType()) {
|
|
|
|
// Friend classes are available as the TypeSourceInfo of the FriendDecl.
|
|
|
|
if (CXXRecordDecl *FriendD = TSI->getType()->getAsCXXRecordDecl())
|
|
|
|
Complete = MethodsAndNestedClassesComplete(FriendD, MNCComplete);
|
|
|
|
else
|
|
|
|
Complete = false;
|
|
|
|
} else {
|
|
|
|
// Friend functions are available through the NamedDecl of FriendDecl.
|
|
|
|
if (const FunctionDecl *FD =
|
|
|
|
dyn_cast<FunctionDecl>((*I)->getFriendDecl()))
|
|
|
|
Complete = FD->isDefined();
|
|
|
|
else
|
|
|
|
// This is a template friend, give up.
|
|
|
|
Complete = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RecordsComplete[RD] = Complete;
|
|
|
|
return Complete;
|
|
|
|
}
|
|
|
|
|
2008-08-23 11:19:52 +08:00
|
|
|
/// ActOnEndOfTranslationUnit - This is called at the very end of the
|
|
|
|
/// translation unit when EOF is reached and all but the top-level scope is
|
|
|
|
/// popped.
|
2010-08-05 17:48:16 +08:00
|
|
|
void Sema::ActOnEndOfTranslationUnit() {
|
2012-05-07 14:16:41 +08:00
|
|
|
assert(DelayedDiagnostics.getCurrentPool() == NULL
|
|
|
|
&& "reached end of translation unit with a pool attached?");
|
|
|
|
|
2012-08-18 06:17:36 +08:00
|
|
|
// If code completion is enabled, don't perform any end-of-translation-unit
|
|
|
|
// work.
|
|
|
|
if (PP.isCodeCompletionEnabled())
|
|
|
|
return;
|
|
|
|
|
When we perform dependent name lookup during template instantiation, it's not
sufficient to only consider names visible at the point of instantiation,
because that may not include names that were visible when the template was
defined. More generally, if the instantiation backtrace goes through a module
M, then every declaration visible within M should be available to the
instantiation. Any of those declarations might be part of the interface that M
intended to export to a template that it instantiates.
The fix here has two parts:
1) If we find a non-visible declaration during name lookup during template
instantiation, check whether the declaration was visible from the defining
module of all entities on the active template instantiation stack. The defining
module is not the owning module in all cases: we look at the module in which a
template was defined, not the module in which it was first instantiated.
2) Perform pending instantiations at the end of a module, not at the end of the
translation unit. This is general goodness, since it significantly cuts down
the amount of redundant work that is performed in every TU importing a module,
and also implicitly adds the module containing the point of instantiation to
the set of modules checked for declarations in a lookup within a template
instantiation.
There's a known issue here with template instantiations performed while
building a module, if additional imports are added later on. I'll fix that
in a subsequent commit.
llvm-svn: 187167
2013-07-26 07:08:39 +08:00
|
|
|
// Complete translation units and modules define vtables and perform implicit
|
|
|
|
// instantiations. PCH files do not.
|
|
|
|
if (TUKind != TU_Prefix) {
|
2012-02-08 00:50:53 +08:00
|
|
|
DiagnoseUseOfUnimplementedSelectors();
|
|
|
|
|
2010-12-13 05:36:11 +08:00
|
|
|
// If any dynamic classes have their key function defined within
|
|
|
|
// this translation unit, then those vtables are considered "used" and must
|
|
|
|
// be emitted.
|
2011-07-28 08:53:40 +08:00
|
|
|
for (DynamicClassesType::iterator I = DynamicClasses.begin(ExternalSource),
|
|
|
|
E = DynamicClasses.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
assert(!(*I)->isDependentType() &&
|
2011-01-26 02:08:22 +08:00
|
|
|
"Should not see dependent types here!");
|
2014-05-04 05:57:40 +08:00
|
|
|
if (const CXXMethodDecl *KeyFunction =
|
|
|
|
Context.getCurrentKeyFunction(*I)) {
|
2010-12-13 05:36:11 +08:00
|
|
|
const FunctionDecl *Definition = 0;
|
|
|
|
if (KeyFunction->hasBody(Definition))
|
2011-07-28 08:53:40 +08:00
|
|
|
MarkVTableUsed(Definition->getLocation(), *I, true);
|
2010-12-13 05:36:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-31 15:58:42 +08:00
|
|
|
// If DefinedUsedVTables ends up marking any virtual member functions it
|
|
|
|
// might lead to more pending template instantiations, which we then need
|
|
|
|
// to instantiate.
|
|
|
|
DefineUsedVTables();
|
|
|
|
|
|
|
|
// C++: Perform implicit template instantiations.
|
|
|
|
//
|
|
|
|
// FIXME: When we perform these implicit instantiations, we do not
|
|
|
|
// carefully keep track of the point of instantiation (C++ [temp.point]).
|
|
|
|
// This means that name lookup that occurs within the template
|
|
|
|
// instantiation will always happen at the end of the translation unit,
|
When we perform dependent name lookup during template instantiation, it's not
sufficient to only consider names visible at the point of instantiation,
because that may not include names that were visible when the template was
defined. More generally, if the instantiation backtrace goes through a module
M, then every declaration visible within M should be available to the
instantiation. Any of those declarations might be part of the interface that M
intended to export to a template that it instantiates.
The fix here has two parts:
1) If we find a non-visible declaration during name lookup during template
instantiation, check whether the declaration was visible from the defining
module of all entities on the active template instantiation stack. The defining
module is not the owning module in all cases: we look at the module in which a
template was defined, not the module in which it was first instantiated.
2) Perform pending instantiations at the end of a module, not at the end of the
translation unit. This is general goodness, since it significantly cuts down
the amount of redundant work that is performed in every TU importing a module,
and also implicitly adds the module containing the point of instantiation to
the set of modules checked for declarations in a lookup within a template
instantiation.
There's a known issue here with template instantiations performed while
building a module, if additional imports are added later on. I'll fix that
in a subsequent commit.
llvm-svn: 187167
2013-07-26 07:08:39 +08:00
|
|
|
// so it will find some names that are not required to be found. This is
|
|
|
|
// valid, but we could do better by diagnosing if an instantiation uses a
|
|
|
|
// name that was not visible at its first point of instantiation.
|
2014-03-22 09:43:32 +08:00
|
|
|
if (ExternalSource) {
|
|
|
|
// Load pending instantiations from the external source.
|
|
|
|
SmallVector<PendingImplicitInstantiation, 4> Pending;
|
|
|
|
ExternalSource->ReadPendingInstantiations(Pending);
|
|
|
|
PendingInstantiations.insert(PendingInstantiations.begin(),
|
|
|
|
Pending.begin(), Pending.end());
|
|
|
|
}
|
2011-05-31 15:58:42 +08:00
|
|
|
PerformPendingInstantiations();
|
2014-03-22 09:43:32 +08:00
|
|
|
|
2013-10-18 13:54:19 +08:00
|
|
|
CheckDelayedMemberExceptionSpecs();
|
2010-11-25 08:35:20 +08:00
|
|
|
}
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2013-10-18 13:54:19 +08:00
|
|
|
// All delayed member exception specs should be checked or we end up accepting
|
|
|
|
// incompatible declarations.
|
|
|
|
assert(DelayedDefaultedMemberExceptionSpecs.empty());
|
|
|
|
assert(DelayedDestructorExceptionSpecChecks.empty());
|
|
|
|
|
2010-08-14 02:42:17 +08:00
|
|
|
// Remove file scoped decls that turned out to be used.
|
2013-04-30 14:43:16 +08:00
|
|
|
UnusedFileScopedDecls.erase(
|
|
|
|
std::remove_if(UnusedFileScopedDecls.begin(0, true),
|
|
|
|
UnusedFileScopedDecls.end(),
|
|
|
|
std::bind1st(std::ptr_fun(ShouldRemoveFromUnused), this)),
|
|
|
|
UnusedFileScopedDecls.end());
|
2010-04-10 01:41:13 +08:00
|
|
|
|
2011-08-26 06:30:56 +08:00
|
|
|
if (TUKind == TU_Prefix) {
|
|
|
|
// Translation unit prefixes don't need any of the checking below.
|
2010-08-14 06:48:40 +08:00
|
|
|
TUScope = 0;
|
2010-08-05 17:48:08 +08:00
|
|
|
return;
|
2010-08-14 06:48:40 +08:00
|
|
|
}
|
2010-08-05 17:48:08 +08:00
|
|
|
|
2009-09-09 02:19:27 +08:00
|
|
|
// Check for #pragma weak identifiers that were never declared
|
|
|
|
// FIXME: This will cause diagnostics to be emitted in a non-determinstic
|
|
|
|
// order! Iterating over a densemap like this is bad.
|
2011-07-29 02:09:57 +08:00
|
|
|
LoadExternalWeakUndeclaredIdentifiers();
|
2009-07-30 11:15:39 +08:00
|
|
|
for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
|
2009-09-09 02:19:27 +08:00
|
|
|
I = WeakUndeclaredIdentifiers.begin(),
|
|
|
|
E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
|
|
|
|
if (I->second.getUsed()) continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-09 02:19:27 +08:00
|
|
|
Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
|
|
|
|
<< I->first;
|
2009-07-30 11:15:39 +08:00
|
|
|
}
|
|
|
|
|
2013-03-14 12:44:56 +08:00
|
|
|
if (LangOpts.CPlusPlus11 &&
|
|
|
|
Diags.getDiagnosticLevel(diag::warn_delegating_ctor_cycle,
|
|
|
|
SourceLocation())
|
|
|
|
!= DiagnosticsEngine::Ignored)
|
|
|
|
CheckDelegatingCtorCycles();
|
|
|
|
|
2011-08-26 06:30:56 +08:00
|
|
|
if (TUKind == TU_Module) {
|
2011-12-02 09:47:07 +08:00
|
|
|
// If we are building a module, resolve all of the exported declarations
|
|
|
|
// now.
|
|
|
|
if (Module *CurrentModule = PP.getCurrentModule()) {
|
|
|
|
ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallVector<Module *, 2> Stack;
|
2011-12-02 09:47:07 +08:00
|
|
|
Stack.push_back(CurrentModule);
|
|
|
|
while (!Stack.empty()) {
|
2013-08-24 00:11:15 +08:00
|
|
|
Module *Mod = Stack.pop_back_val();
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2013-03-21 05:10:35 +08:00
|
|
|
// Resolve the exported declarations and conflicts.
|
2011-12-02 09:47:07 +08:00
|
|
|
// FIXME: Actually complain, once we figure out how to teach the
|
2013-03-21 05:10:35 +08:00
|
|
|
// diagnostic client to deal with complaints in the module map at this
|
2011-12-02 09:47:07 +08:00
|
|
|
// point.
|
|
|
|
ModMap.resolveExports(Mod, /*Complain=*/false);
|
2013-09-24 17:14:14 +08:00
|
|
|
ModMap.resolveUses(Mod, /*Complain=*/false);
|
2013-03-21 05:10:35 +08:00
|
|
|
ModMap.resolveConflicts(Mod, /*Complain=*/false);
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2011-12-02 09:47:07 +08:00
|
|
|
// Queue the submodules, so their exports will also be resolved.
|
2012-01-05 07:32:19 +08:00
|
|
|
for (Module::submodule_iterator Sub = Mod->submodule_begin(),
|
|
|
|
SubEnd = Mod->submodule_end();
|
2011-12-02 09:47:07 +08:00
|
|
|
Sub != SubEnd; ++Sub) {
|
2012-01-05 07:32:19 +08:00
|
|
|
Stack.push_back(*Sub);
|
2011-12-02 09:47:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2011-08-26 06:30:56 +08:00
|
|
|
// Modules don't need any of the checking below.
|
|
|
|
TUScope = 0;
|
|
|
|
return;
|
|
|
|
}
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2009-03-11 07:43:53 +08:00
|
|
|
// C99 6.9.2p2:
|
|
|
|
// A declaration of an identifier for an object that has file
|
|
|
|
// scope without an initializer, and without a storage-class
|
|
|
|
// specifier or with the storage-class specifier static,
|
|
|
|
// constitutes a tentative definition. If a translation unit
|
|
|
|
// contains one or more tentative definitions for an identifier,
|
|
|
|
// and the translation unit contains no external definition for
|
|
|
|
// that identifier, then the behavior is exactly as if the
|
|
|
|
// translation unit contains a file scope declaration of that
|
|
|
|
// identifier, with the composite type as of the end of the
|
|
|
|
// translation unit, with an initializer equal to 0.
|
2010-02-01 06:27:38 +08:00
|
|
|
llvm::SmallSet<VarDecl *, 32> Seen;
|
2013-01-20 09:04:14 +08:00
|
|
|
for (TentativeDefinitionsType::iterator
|
2011-07-28 04:58:46 +08:00
|
|
|
T = TentativeDefinitions.begin(ExternalSource),
|
|
|
|
TEnd = TentativeDefinitions.end();
|
2013-01-20 09:04:14 +08:00
|
|
|
T != TEnd; ++T)
|
2011-07-28 04:58:46 +08:00
|
|
|
{
|
|
|
|
VarDecl *VD = (*T)->getActingDefinition();
|
2010-02-01 06:27:38 +08:00
|
|
|
|
|
|
|
// If the tentative definition was completed, getActingDefinition() returns
|
|
|
|
// null. If we've already seen this variable before, insert()'s second
|
|
|
|
// return value is false.
|
|
|
|
if (VD == 0 || VD->isInvalidDecl() || !Seen.insert(VD))
|
2009-04-22 01:11:58 +08:00
|
|
|
continue;
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
if (const IncompleteArrayType *ArrayT
|
2009-04-22 01:11:58 +08:00
|
|
|
= Context.getAsIncompleteArrayType(VD->getType())) {
|
2009-09-09 02:19:27 +08:00
|
|
|
// Set the length of the array to 1 (C99 6.9.2p5).
|
|
|
|
Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
|
|
|
|
llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
|
2009-10-16 08:14:28 +08:00
|
|
|
QualType T = Context.getConstantArrayType(ArrayT->getElementType(),
|
|
|
|
One, ArrayType::Normal, 0);
|
2009-09-09 02:19:27 +08:00
|
|
|
VD->setType(T);
|
2009-09-09 23:08:12 +08:00
|
|
|
} else if (RequireCompleteType(VD->getLocation(), VD->getType(),
|
2009-04-22 01:11:58 +08:00
|
|
|
diag::err_tentative_def_incomplete_type))
|
|
|
|
VD->setInvalidDecl();
|
|
|
|
|
2012-10-24 04:19:32 +08:00
|
|
|
CheckCompleteVariableDeclaration(VD);
|
|
|
|
|
2009-04-22 01:11:58 +08:00
|
|
|
// Notify the consumer that we've completed a tentative definition.
|
|
|
|
if (!VD->isInvalidDecl())
|
|
|
|
Consumer.CompleteTentativeDefinition(VD);
|
|
|
|
|
2009-03-11 07:43:53 +08:00
|
|
|
}
|
2011-01-31 15:04:37 +08:00
|
|
|
|
|
|
|
// If there were errors, disable 'unused' warnings since they will mostly be
|
|
|
|
// noise.
|
|
|
|
if (!Diags.hasErrorOccurred()) {
|
|
|
|
// Output warning for unused file scoped decls.
|
2011-07-28 05:45:57 +08:00
|
|
|
for (UnusedFileScopedDeclsType::iterator
|
|
|
|
I = UnusedFileScopedDecls.begin(ExternalSource),
|
2011-01-31 15:04:37 +08:00
|
|
|
E = UnusedFileScopedDecls.end(); I != E; ++I) {
|
2011-07-28 05:45:57 +08:00
|
|
|
if (ShouldRemoveFromUnused(this, *I))
|
|
|
|
continue;
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2011-01-31 15:04:37 +08:00
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
|
|
|
|
const FunctionDecl *DiagD;
|
|
|
|
if (!FD->hasBody(DiagD))
|
|
|
|
DiagD = FD;
|
2011-03-04 01:47:42 +08:00
|
|
|
if (DiagD->isDeleted())
|
|
|
|
continue; // Deleted functions are supposed to be unused.
|
2011-04-20 03:51:10 +08:00
|
|
|
if (DiagD->isReferenced()) {
|
|
|
|
if (isa<CXXMethodDecl>(DiagD))
|
|
|
|
Diag(DiagD->getLocation(), diag::warn_unneeded_member_function)
|
|
|
|
<< DiagD->getDeclName();
|
2012-06-28 03:43:29 +08:00
|
|
|
else {
|
2013-04-04 03:27:57 +08:00
|
|
|
if (FD->getStorageClass() == SC_Static &&
|
2012-06-28 03:43:29 +08:00
|
|
|
!FD->isInlineSpecified() &&
|
2013-08-22 08:27:10 +08:00
|
|
|
!SourceMgr.isInMainFile(
|
2012-06-28 03:43:29 +08:00
|
|
|
SourceMgr.getExpansionLoc(FD->getLocation())))
|
|
|
|
Diag(DiagD->getLocation(), diag::warn_unneeded_static_internal_decl)
|
|
|
|
<< DiagD->getDeclName();
|
|
|
|
else
|
|
|
|
Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl)
|
|
|
|
<< /*function*/0 << DiagD->getDeclName();
|
|
|
|
}
|
2011-04-20 03:51:10 +08:00
|
|
|
} else {
|
|
|
|
Diag(DiagD->getLocation(),
|
|
|
|
isa<CXXMethodDecl>(DiagD) ? diag::warn_unused_member_function
|
|
|
|
: diag::warn_unused_function)
|
|
|
|
<< DiagD->getDeclName();
|
|
|
|
}
|
2011-01-31 15:04:37 +08:00
|
|
|
} else {
|
|
|
|
const VarDecl *DiagD = cast<VarDecl>(*I)->getDefinition();
|
|
|
|
if (!DiagD)
|
|
|
|
DiagD = cast<VarDecl>(*I);
|
2011-04-20 03:51:10 +08:00
|
|
|
if (DiagD->isReferenced()) {
|
|
|
|
Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl)
|
|
|
|
<< /*variable*/1 << DiagD->getDeclName();
|
2013-09-11 18:37:35 +08:00
|
|
|
} else if (DiagD->getType().isConstQualified()) {
|
|
|
|
Diag(DiagD->getLocation(), diag::warn_unused_const_variable)
|
|
|
|
<< DiagD->getDeclName();
|
2013-09-10 11:05:56 +08:00
|
|
|
} else {
|
2011-04-20 03:51:10 +08:00
|
|
|
Diag(DiagD->getLocation(), diag::warn_unused_variable)
|
2013-04-10 08:47:10 +08:00
|
|
|
<< DiagD->getDeclName();
|
2011-04-20 03:51:10 +08:00
|
|
|
}
|
2011-01-31 15:04:37 +08:00
|
|
|
}
|
2010-08-15 09:15:20 +08:00
|
|
|
}
|
2011-02-19 10:53:41 +08:00
|
|
|
|
2013-01-26 08:35:08 +08:00
|
|
|
if (ExternalSource)
|
2013-02-01 16:13:20 +08:00
|
|
|
ExternalSource->ReadUndefinedButUsed(UndefinedButUsed);
|
|
|
|
checkUndefinedButUsed(*this);
|
2010-08-14 02:42:17 +08:00
|
|
|
}
|
2010-08-14 06:48:40 +08:00
|
|
|
|
2012-06-06 16:32:04 +08:00
|
|
|
if (Diags.getDiagnosticLevel(diag::warn_unused_private_field,
|
|
|
|
SourceLocation())
|
|
|
|
!= DiagnosticsEngine::Ignored) {
|
|
|
|
RecordCompleteMap RecordsComplete;
|
|
|
|
RecordCompleteMap MNCComplete;
|
|
|
|
for (NamedDeclSetType::iterator I = UnusedPrivateFields.begin(),
|
|
|
|
E = UnusedPrivateFields.end(); I != E; ++I) {
|
|
|
|
const NamedDecl *D = *I;
|
|
|
|
const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext());
|
|
|
|
if (RD && !RD->isUnion() &&
|
|
|
|
IsRecordFullyDefined(RD, RecordsComplete, MNCComplete)) {
|
|
|
|
Diag(D->getLocation(), diag::warn_unused_private_field)
|
|
|
|
<< D->getDeclName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-22 04:05:19 +08:00
|
|
|
// Check we've noticed that we're no longer parsing the initializer for every
|
|
|
|
// variable. If we miss cases, then at best we have a performance issue and
|
|
|
|
// at worst a rejects-valid bug.
|
|
|
|
assert(ParsingInitForAutoVars.empty() &&
|
|
|
|
"Didn't unmark var as having its initializer parsed");
|
|
|
|
|
2010-08-14 06:48:40 +08:00
|
|
|
TUScope = 0;
|
2008-08-23 11:19:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-10 13:17:58 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Helper functions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-08-09 01:45:02 +08:00
|
|
|
DeclContext *Sema::getFunctionLevelDeclContext() {
|
2009-12-19 18:53:49 +08:00
|
|
|
DeclContext *DC = CurContext;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-01-07 12:59:52 +08:00
|
|
|
while (true) {
|
2013-04-17 03:37:38 +08:00
|
|
|
if (isa<BlockDecl>(DC) || isa<EnumDecl>(DC) || isa<CapturedDecl>(DC)) {
|
2012-01-07 12:59:52 +08:00
|
|
|
DC = DC->getParent();
|
|
|
|
} else if (isa<CXXMethodDecl>(DC) &&
|
2012-02-13 01:34:23 +08:00
|
|
|
cast<CXXMethodDecl>(DC)->getOverloadedOperator() == OO_Call &&
|
2012-01-07 12:59:52 +08:00
|
|
|
cast<CXXRecordDecl>(DC->getParent())->isLambda()) {
|
|
|
|
DC = DC->getParent()->getParent();
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-09 01:45:02 +08:00
|
|
|
return DC;
|
|
|
|
}
|
|
|
|
|
2008-12-05 07:50:19 +08:00
|
|
|
/// getCurFunctionDecl - If inside of a function body, this returns a pointer
|
|
|
|
/// to the function decl for the function being parsed. If we're currently
|
|
|
|
/// in a 'block', this returns the containing context.
|
|
|
|
FunctionDecl *Sema::getCurFunctionDecl() {
|
2009-08-09 01:45:02 +08:00
|
|
|
DeclContext *DC = getFunctionLevelDeclContext();
|
2008-12-05 07:50:19 +08:00
|
|
|
return dyn_cast<FunctionDecl>(DC);
|
|
|
|
}
|
|
|
|
|
2008-08-11 13:35:13 +08:00
|
|
|
ObjCMethodDecl *Sema::getCurMethodDecl() {
|
2009-08-09 01:45:02 +08:00
|
|
|
DeclContext *DC = getFunctionLevelDeclContext();
|
2013-06-01 05:51:12 +08:00
|
|
|
while (isa<RecordDecl>(DC))
|
|
|
|
DC = DC->getParent();
|
2008-11-18 00:28:52 +08:00
|
|
|
return dyn_cast<ObjCMethodDecl>(DC);
|
2008-08-11 13:35:13 +08:00
|
|
|
}
|
2008-12-05 07:50:19 +08:00
|
|
|
|
|
|
|
NamedDecl *Sema::getCurFunctionOrMethodDecl() {
|
2009-08-09 01:45:02 +08:00
|
|
|
DeclContext *DC = getFunctionLevelDeclContext();
|
2008-12-05 07:50:19 +08:00
|
|
|
if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
|
2009-01-20 09:17:11 +08:00
|
|
|
return cast<NamedDecl>(DC);
|
2008-12-05 07:50:19 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-14 17:49:32 +08:00
|
|
|
void Sema::EmitCurrentDiagnostic(unsigned DiagID) {
|
|
|
|
// FIXME: It doesn't make sense to me that DiagID is an incoming argument here
|
|
|
|
// and yet we also use the current diag ID on the DiagnosticsEngine. This has
|
|
|
|
// been made more painfully obvious by the refactor that introduced this
|
|
|
|
// function, but it is possible that the incoming argument can be
|
|
|
|
// eliminnated. If it truly cannot be (for example, there is some reentrancy
|
|
|
|
// issue I am not seeing yet), then there should at least be a clarifying
|
|
|
|
// comment somewhere.
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<TemplateDeductionInfo*> Info = isSFINAEContext()) {
|
2012-03-14 17:49:32 +08:00
|
|
|
switch (DiagnosticIDs::getDiagnosticSFINAEResponse(
|
|
|
|
Diags.getCurrentDiagID())) {
|
2010-11-19 04:06:41 +08:00
|
|
|
case DiagnosticIDs::SFINAE_Report:
|
2011-10-19 08:07:01 +08:00
|
|
|
// We'll report the diagnostic below.
|
2010-10-13 07:32:35 +08:00
|
|
|
break;
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2011-10-19 08:07:01 +08:00
|
|
|
case DiagnosticIDs::SFINAE_SubstitutionFailure:
|
|
|
|
// Count this failure so that we know that template argument deduction
|
|
|
|
// has failed.
|
2012-03-14 17:49:32 +08:00
|
|
|
++NumSFINAEErrors;
|
2012-05-07 17:03:25 +08:00
|
|
|
|
|
|
|
// Make a copy of this suppressed diagnostic and store it with the
|
|
|
|
// template-deduction information.
|
|
|
|
if (*Info && !(*Info)->hasSFINAEDiagnostic()) {
|
|
|
|
Diagnostic DiagInfo(&Diags);
|
|
|
|
(*Info)->addSFINAEDiagnostic(DiagInfo.getLocation(),
|
|
|
|
PartialDiagnostic(DiagInfo, Context.getDiagAllocator()));
|
|
|
|
}
|
|
|
|
|
2012-03-14 17:49:32 +08:00
|
|
|
Diags.setLastDiagnosticIgnored();
|
|
|
|
Diags.Clear();
|
2011-10-19 08:07:01 +08:00
|
|
|
return;
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2011-10-19 08:07:01 +08:00
|
|
|
case DiagnosticIDs::SFINAE_AccessControl: {
|
2011-05-12 07:45:11 +08:00
|
|
|
// Per C++ Core Issue 1170, access control is part of SFINAE.
|
2012-03-14 02:30:54 +08:00
|
|
|
// Additionally, the AccessCheckingSFINAE flag can be used to temporarily
|
2011-05-12 07:45:11 +08:00
|
|
|
// make access control a part of SFINAE for the purposes of checking
|
|
|
|
// type traits.
|
2013-01-02 19:42:31 +08:00
|
|
|
if (!AccessCheckingSFINAE && !getLangOpts().CPlusPlus11)
|
2011-01-28 06:31:44 +08:00
|
|
|
break;
|
2011-10-19 08:07:01 +08:00
|
|
|
|
2012-03-14 17:49:32 +08:00
|
|
|
SourceLocation Loc = Diags.getCurrentDiagLoc();
|
2011-10-19 08:07:01 +08:00
|
|
|
|
|
|
|
// Suppress this diagnostic.
|
2012-03-14 17:49:32 +08:00
|
|
|
++NumSFINAEErrors;
|
2012-05-07 17:03:25 +08:00
|
|
|
|
|
|
|
// Make a copy of this suppressed diagnostic and store it with the
|
|
|
|
// template-deduction information.
|
|
|
|
if (*Info && !(*Info)->hasSFINAEDiagnostic()) {
|
|
|
|
Diagnostic DiagInfo(&Diags);
|
|
|
|
(*Info)->addSFINAEDiagnostic(DiagInfo.getLocation(),
|
|
|
|
PartialDiagnostic(DiagInfo, Context.getDiagAllocator()));
|
|
|
|
}
|
|
|
|
|
2012-03-14 17:49:32 +08:00
|
|
|
Diags.setLastDiagnosticIgnored();
|
|
|
|
Diags.Clear();
|
2011-10-19 08:07:01 +08:00
|
|
|
|
|
|
|
// Now the diagnostic state is clear, produce a C++98 compatibility
|
|
|
|
// warning.
|
2012-03-14 17:49:32 +08:00
|
|
|
Diag(Loc, diag::warn_cxx98_compat_sfinae_access_control);
|
2011-10-19 08:07:01 +08:00
|
|
|
|
|
|
|
// The last diagnostic which Sema produced was ignored. Suppress any
|
|
|
|
// notes attached to it.
|
2012-03-14 17:49:32 +08:00
|
|
|
Diags.setLastDiagnosticIgnored();
|
2010-10-13 07:32:35 +08:00
|
|
|
return;
|
2011-10-19 08:07:01 +08:00
|
|
|
}
|
|
|
|
|
2010-11-19 04:06:41 +08:00
|
|
|
case DiagnosticIDs::SFINAE_Suppress:
|
2010-10-13 07:32:35 +08:00
|
|
|
// Make a copy of this suppressed diagnostic and store it with the
|
|
|
|
// template-deduction information;
|
2012-05-07 17:03:25 +08:00
|
|
|
if (*Info) {
|
|
|
|
Diagnostic DiagInfo(&Diags);
|
2011-01-28 06:31:44 +08:00
|
|
|
(*Info)->addSuppressedDiagnostic(DiagInfo.getLocation(),
|
2012-05-07 17:03:25 +08:00
|
|
|
PartialDiagnostic(DiagInfo, Context.getDiagAllocator()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Suppress this diagnostic.
|
2012-03-14 17:49:32 +08:00
|
|
|
Diags.setLastDiagnosticIgnored();
|
|
|
|
Diags.Clear();
|
2010-10-13 07:32:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2011-09-28 07:30:47 +08:00
|
|
|
// Set up the context's printing policy based on our current state.
|
2012-03-14 17:49:32 +08:00
|
|
|
Context.setPrintingPolicy(getPrintingPolicy());
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2010-10-13 07:32:35 +08:00
|
|
|
// Emit the diagnostic.
|
2012-03-14 17:49:32 +08:00
|
|
|
if (!Diags.EmitCurrentDiagnostic())
|
2009-06-14 15:33:30 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-21 06:48:49 +08:00
|
|
|
// If this is not a note, and we're in a template instantiation
|
|
|
|
// that is different from the last template instantiation where
|
|
|
|
// we emitted an error, print a template instantiation
|
|
|
|
// backtrace.
|
2010-11-19 04:06:41 +08:00
|
|
|
if (!DiagnosticIDs::isBuiltinNote(DiagID) &&
|
2012-03-14 17:49:32 +08:00
|
|
|
!ActiveTemplateInstantiations.empty() &&
|
|
|
|
ActiveTemplateInstantiations.back()
|
|
|
|
!= LastTemplateInstantiationErrorContext) {
|
|
|
|
PrintInstantiationStack();
|
|
|
|
LastTemplateInstantiationErrorContext = ActiveTemplateInstantiations.back();
|
2009-03-21 06:48:49 +08:00
|
|
|
}
|
|
|
|
}
|
Add support for retrieving the Doxygen comment associated with a given
declaration in the AST.
The new ASTContext::getCommentForDecl function searches for a comment
that is attached to the given declaration, and returns that comment,
which may be composed of several comment blocks.
Comments are always available in an AST. However, to avoid harming
performance, we don't actually parse the comments. Rather, we keep the
source ranges of all of the comments within a large, sorted vector,
then lazily extract comments via a binary search in that vector only
when needed (which never occurs in a "normal" compile).
Comments are written to a precompiled header/AST file as a blob of
source ranges. That blob is only lazily loaded when one requests a
comment for a declaration (this never occurs in a "normal" compile).
The indexer testbed now supports comment extraction. When the
-point-at location points to a declaration with a Doxygen-style
comment, the indexer testbed prints the associated comment
block(s). See test/Index/comments.c for an example.
Some notes:
- We don't actually attempt to parse the comment blocks themselves,
beyond identifying them as Doxygen comment blocks to associate them
with a declaration.
- We won't find comment blocks that aren't adjacent to the
declaration, because we start our search based on the location of
the declaration.
- We don't go through the necessary hops to find, for example,
whether some redeclaration of a declaration has comments when our
current declaration does not. Similarly, we don't attempt to
associate a \param Foo marker in a function body comment with the
parameter named Foo (although that is certainly possible).
- Verification of my "no performance impact" claims is still "to be
done".
llvm-svn: 74704
2009-07-03 01:08:52 +08:00
|
|
|
|
2009-08-27 06:33:56 +08:00
|
|
|
Sema::SemaDiagnosticBuilder
|
|
|
|
Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) {
|
|
|
|
SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID()));
|
|
|
|
PD.Emit(Builder);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-27 06:33:56 +08:00
|
|
|
return Builder;
|
|
|
|
}
|
|
|
|
|
2011-07-26 13:40:03 +08:00
|
|
|
/// \brief Looks through the macro-expansion chain for the given
|
|
|
|
/// location, looking for a macro expansion with the given name.
|
2011-03-08 15:59:04 +08:00
|
|
|
/// If one is found, returns true and sets the location to that
|
2011-07-26 13:40:03 +08:00
|
|
|
/// expansion loc.
|
2011-07-23 18:55:15 +08:00
|
|
|
bool Sema::findMacroSpelling(SourceLocation &locref, StringRef name) {
|
2011-03-08 15:59:04 +08:00
|
|
|
SourceLocation loc = locref;
|
|
|
|
if (!loc.isMacroID()) return false;
|
|
|
|
|
|
|
|
// There's no good way right now to look at the intermediate
|
2011-07-26 13:40:03 +08:00
|
|
|
// expansions, so just jump to the expansion location.
|
2011-07-26 00:49:02 +08:00
|
|
|
loc = getSourceManager().getExpansionLoc(loc);
|
2011-03-08 15:59:04 +08:00
|
|
|
|
|
|
|
// If that's written with the name, stop here.
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<char, 16> buffer;
|
2011-03-08 15:59:04 +08:00
|
|
|
if (getPreprocessor().getSpelling(loc, buffer) == name) {
|
|
|
|
locref = loc;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-03 01:43:08 +08:00
|
|
|
/// \brief Determines the active Scope associated with the given declaration
|
|
|
|
/// context.
|
|
|
|
///
|
|
|
|
/// This routine maps a declaration context to the active Scope object that
|
|
|
|
/// represents that declaration context in the parser. It is typically used
|
|
|
|
/// from "scope-less" code (e.g., template instantiation, lazy creation of
|
|
|
|
/// declarations) that injects a name for name-lookup purposes and, therefore,
|
|
|
|
/// must update the Scope.
|
|
|
|
///
|
|
|
|
/// \returns The scope corresponding to the given declaraion context, or NULL
|
|
|
|
/// if no such scope is open.
|
|
|
|
Scope *Sema::getScopeForContext(DeclContext *Ctx) {
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2010-07-03 01:43:08 +08:00
|
|
|
if (!Ctx)
|
|
|
|
return 0;
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2010-07-03 01:43:08 +08:00
|
|
|
Ctx = Ctx->getPrimaryContext();
|
|
|
|
for (Scope *S = getCurScope(); S; S = S->getParent()) {
|
2010-07-09 07:07:34 +08:00
|
|
|
// Ignore scopes that cannot have declarations. This is important for
|
|
|
|
// out-of-line definitions of static class members.
|
|
|
|
if (S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope))
|
2013-10-09 01:08:03 +08:00
|
|
|
if (DeclContext *Entity = S->getEntity())
|
2010-07-09 07:07:34 +08:00
|
|
|
if (Ctx == Entity->getPrimaryContext())
|
|
|
|
return S;
|
2010-07-03 01:43:08 +08:00
|
|
|
}
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2010-07-03 01:43:08 +08:00
|
|
|
return 0;
|
|
|
|
}
|
Keep an explicit stack of function and block scopes, each element of
which has the label map, switch statement stack, etc. Previously, we
had a single set of maps in Sema (for the function) along with a stack
of block scopes. However, this lead to funky behavior with nested
functions, e.g., in the member functions of local classes.
The explicit-stack approach is far cleaner, and we retain a 1-element
cache so that we're not malloc/free'ing every time we enter a
function. Fixes PR6382.
Also, tweaked the unused-variable warning suppression logic to look at
errors within a given Scope rather than within a given function. The
prior code wasn't looking at the right number-of-errors count when
dealing with blocks, since the block's count would be deallocated
before we got to ActOnPopScope. This approach works with nested
blocks/functions, and gives tighter error recovery.
llvm-svn: 97518
2010-03-02 07:15:13 +08:00
|
|
|
|
|
|
|
/// \brief Enter a new function scope
|
|
|
|
void Sema::PushFunctionScope() {
|
2010-08-25 16:40:02 +08:00
|
|
|
if (FunctionScopes.size() == 1) {
|
|
|
|
// Use the "top" function scope rather than having to allocate
|
|
|
|
// memory for a new scope.
|
2010-11-19 08:19:15 +08:00
|
|
|
FunctionScopes.back()->Clear();
|
2010-08-25 16:40:02 +08:00
|
|
|
FunctionScopes.push_back(FunctionScopes.back());
|
Keep an explicit stack of function and block scopes, each element of
which has the label map, switch statement stack, etc. Previously, we
had a single set of maps in Sema (for the function) along with a stack
of block scopes. However, this lead to funky behavior with nested
functions, e.g., in the member functions of local classes.
The explicit-stack approach is far cleaner, and we retain a 1-element
cache so that we're not malloc/free'ing every time we enter a
function. Fixes PR6382.
Also, tweaked the unused-variable warning suppression logic to look at
errors within a given Scope rather than within a given function. The
prior code wasn't looking at the right number-of-errors count when
dealing with blocks, since the block's count would be deallocated
before we got to ActOnPopScope. This approach works with nested
blocks/functions, and gives tighter error recovery.
llvm-svn: 97518
2010-03-02 07:15:13 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2010-11-19 08:19:15 +08:00
|
|
|
FunctionScopes.push_back(new FunctionScopeInfo(getDiagnostics()));
|
Keep an explicit stack of function and block scopes, each element of
which has the label map, switch statement stack, etc. Previously, we
had a single set of maps in Sema (for the function) along with a stack
of block scopes. However, this lead to funky behavior with nested
functions, e.g., in the member functions of local classes.
The explicit-stack approach is far cleaner, and we retain a 1-element
cache so that we're not malloc/free'ing every time we enter a
function. Fixes PR6382.
Also, tweaked the unused-variable warning suppression logic to look at
errors within a given Scope rather than within a given function. The
prior code wasn't looking at the right number-of-errors count when
dealing with blocks, since the block's count would be deallocated
before we got to ActOnPopScope. This approach works with nested
blocks/functions, and gives tighter error recovery.
llvm-svn: 97518
2010-03-02 07:15:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::PushBlockScope(Scope *BlockScope, BlockDecl *Block) {
|
2010-11-19 08:19:15 +08:00
|
|
|
FunctionScopes.push_back(new BlockScopeInfo(getDiagnostics(),
|
Keep an explicit stack of function and block scopes, each element of
which has the label map, switch statement stack, etc. Previously, we
had a single set of maps in Sema (for the function) along with a stack
of block scopes. However, this lead to funky behavior with nested
functions, e.g., in the member functions of local classes.
The explicit-stack approach is far cleaner, and we retain a 1-element
cache so that we're not malloc/free'ing every time we enter a
function. Fixes PR6382.
Also, tweaked the unused-variable warning suppression logic to look at
errors within a given Scope rather than within a given function. The
prior code wasn't looking at the right number-of-errors count when
dealing with blocks, since the block's count would be deallocated
before we got to ActOnPopScope. This approach works with nested
blocks/functions, and gives tighter error recovery.
llvm-svn: 97518
2010-03-02 07:15:13 +08:00
|
|
|
BlockScope, Block));
|
|
|
|
}
|
|
|
|
|
2013-11-12 09:46:33 +08:00
|
|
|
LambdaScopeInfo *Sema::PushLambdaScope() {
|
2013-11-12 09:40:44 +08:00
|
|
|
LambdaScopeInfo *const LSI = new LambdaScopeInfo(getDiagnostics());
|
|
|
|
FunctionScopes.push_back(LSI);
|
|
|
|
return LSI;
|
Implement a rudimentary form of generic lambdas.
Specifically, the following features are not included in this commit:
- any sort of capturing within generic lambdas
- generic lambdas within template functions and nested
within other generic lambdas
- conversion operator for captureless lambdas
- ensuring all visitors are generic lambda aware
(Although I have gotten some useful feedback on my patches of the above and will be incorporating that as I submit those patches for commit)
As an example of what compiles through this commit:
template <class F1, class F2>
struct overload : F1, F2 {
using F1::operator();
using F2::operator();
overload(F1 f1, F2 f2) : F1(f1), F2(f2) { }
};
auto Recursive = [](auto Self, auto h, auto ... rest) {
return 1 + Self(Self, rest...);
};
auto Base = [](auto Self, auto h) {
return 1;
};
overload<decltype(Base), decltype(Recursive)> O(Base, Recursive);
int num_params = O(O, 5, 3, "abc", 3.14, 'a');
Please see attached tests for more examples.
This patch has been reviewed by Doug and Richard. Minor changes (non-functionality affecting) have been made since both of them formally looked at it, but the changes involve removal of supernumerary return type deduction changes (since they are now redundant, with richard having committed a recent patch to address return type deduction for C++11 lambdas using C++14 semantics).
Some implementation notes:
- Add a new Declarator context => LambdaExprParameterContext to
clang::Declarator to allow the use of 'auto' in declaring generic
lambda parameters
- Add various helpers to CXXRecordDecl to facilitate identifying
and querying a closure class
- LambdaScopeInfo (which maintains the current lambda's Sema state)
was augmented to house the current depth of the template being
parsed (id est the Parser calls Sema::RecordParsingTemplateParameterDepth)
so that SemaType.cpp::ConvertDeclSpecToType may use it to immediately
generate a template-parameter-type when 'auto' is parsed in a generic
lambda parameter context. (i.e we do NOT use AutoType deduced to
a template parameter type - Richard seemed ok with this approach).
We encode that this template type was generated from an auto by simply
adding $auto to the name which can be used for better diagnostics if needed.
- SemaLambda.h was added to hold some common lambda utility
functions (this file is likely to grow ...)
- Teach Sema::ActOnStartOfFunctionDef to check whether it
is being called to instantiate a generic lambda's call
operator, and if so, push an appropriately prepared
LambdaScopeInfo object on the stack.
- various tests were added - but much more will be needed.
There is obviously more work to be done, and both Richard (weakly) and Doug (strongly)
have requested that LambdaExpr be removed form the CXXRecordDecl LambdaDefinitionaData
in a future patch which is forthcoming.
A greatful thanks to all reviewers including Eli Friedman, James Dennett,
and especially the two gracious wizards (Richard Smith and Doug Gregor)
who spent hours providing feedback (in person in Chicago and on the mailing lists).
And yet I am certain that I have allowed unidentified bugs to creep in; bugs, that I will do my best to slay, once identified!
Thanks!
llvm-svn: 191453
2013-09-27 03:54:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::RecordParsingTemplateParameterDepth(unsigned Depth) {
|
|
|
|
if (LambdaScopeInfo *const LSI = getCurLambda()) {
|
|
|
|
LSI->AutoTemplateParameterDepth = Depth;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
llvm_unreachable(
|
|
|
|
"Remove assertion if intentionally called in a non-lambda context.");
|
2012-01-05 11:35:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::PopFunctionScopeInfo(const AnalysisBasedWarnings::Policy *WP,
|
|
|
|
const Decl *D, const BlockExpr *blkExpr) {
|
2013-01-20 09:04:14 +08:00
|
|
|
FunctionScopeInfo *Scope = FunctionScopes.pop_back_val();
|
2010-08-25 16:40:02 +08:00
|
|
|
assert(!FunctionScopes.empty() && "mismatched push/pop!");
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2011-02-23 09:51:48 +08:00
|
|
|
// Issue any analysis-based warnings.
|
|
|
|
if (WP && D)
|
2011-02-23 09:51:53 +08:00
|
|
|
AnalysisWarnings.IssueWarnings(*WP, Scope, D, blkExpr);
|
2011-02-23 09:52:04 +08:00
|
|
|
else {
|
2011-07-23 18:55:15 +08:00
|
|
|
for (SmallVectorImpl<sema::PossiblyUnreachableDiag>::iterator
|
2011-02-23 09:52:04 +08:00
|
|
|
i = Scope->PossiblyUnreachableDiags.begin(),
|
|
|
|
e = Scope->PossiblyUnreachableDiags.end();
|
|
|
|
i != e; ++i) {
|
|
|
|
const sema::PossiblyUnreachableDiag &D = *i;
|
|
|
|
Diag(D.Loc, D.PD);
|
|
|
|
}
|
|
|
|
}
|
2011-02-23 09:51:48 +08:00
|
|
|
|
2011-02-23 09:52:04 +08:00
|
|
|
if (FunctionScopes.back() != Scope) {
|
2010-08-25 16:40:02 +08:00
|
|
|
delete Scope;
|
2011-02-23 09:52:04 +08:00
|
|
|
}
|
Keep an explicit stack of function and block scopes, each element of
which has the label map, switch statement stack, etc. Previously, we
had a single set of maps in Sema (for the function) along with a stack
of block scopes. However, this lead to funky behavior with nested
functions, e.g., in the member functions of local classes.
The explicit-stack approach is far cleaner, and we retain a 1-element
cache so that we're not malloc/free'ing every time we enter a
function. Fixes PR6382.
Also, tweaked the unused-variable warning suppression logic to look at
errors within a given Scope rather than within a given function. The
prior code wasn't looking at the right number-of-errors count when
dealing with blocks, since the block's count would be deallocated
before we got to ActOnPopScope. This approach works with nested
blocks/functions, and gives tighter error recovery.
llvm-svn: 97518
2010-03-02 07:15:13 +08:00
|
|
|
}
|
|
|
|
|
2012-02-15 06:14:32 +08:00
|
|
|
void Sema::PushCompoundScope() {
|
|
|
|
getCurFunction()->CompoundScopes.push_back(CompoundScopeInfo());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::PopCompoundScope() {
|
|
|
|
FunctionScopeInfo *CurFunction = getCurFunction();
|
|
|
|
assert(!CurFunction->CompoundScopes.empty() && "mismatched push/pop");
|
|
|
|
|
|
|
|
CurFunction->CompoundScopes.pop_back();
|
|
|
|
}
|
|
|
|
|
Keep an explicit stack of function and block scopes, each element of
which has the label map, switch statement stack, etc. Previously, we
had a single set of maps in Sema (for the function) along with a stack
of block scopes. However, this lead to funky behavior with nested
functions, e.g., in the member functions of local classes.
The explicit-stack approach is far cleaner, and we retain a 1-element
cache so that we're not malloc/free'ing every time we enter a
function. Fixes PR6382.
Also, tweaked the unused-variable warning suppression logic to look at
errors within a given Scope rather than within a given function. The
prior code wasn't looking at the right number-of-errors count when
dealing with blocks, since the block's count would be deallocated
before we got to ActOnPopScope. This approach works with nested
blocks/functions, and gives tighter error recovery.
llvm-svn: 97518
2010-03-02 07:15:13 +08:00
|
|
|
/// \brief Determine whether any errors occurred within this function/method/
|
|
|
|
/// block.
|
2011-06-16 07:02:42 +08:00
|
|
|
bool Sema::hasAnyUnrecoverableErrorsInThisFunction() const {
|
|
|
|
return getCurFunction()->ErrorTrap.hasUnrecoverableErrorOccurred();
|
Keep an explicit stack of function and block scopes, each element of
which has the label map, switch statement stack, etc. Previously, we
had a single set of maps in Sema (for the function) along with a stack
of block scopes. However, this lead to funky behavior with nested
functions, e.g., in the member functions of local classes.
The explicit-stack approach is far cleaner, and we retain a 1-element
cache so that we're not malloc/free'ing every time we enter a
function. Fixes PR6382.
Also, tweaked the unused-variable warning suppression logic to look at
errors within a given Scope rather than within a given function. The
prior code wasn't looking at the right number-of-errors count when
dealing with blocks, since the block's count would be deallocated
before we got to ActOnPopScope. This approach works with nested
blocks/functions, and gives tighter error recovery.
llvm-svn: 97518
2010-03-02 07:15:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BlockScopeInfo *Sema::getCurBlock() {
|
|
|
|
if (FunctionScopes.empty())
|
|
|
|
return 0;
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2014-04-27 02:29:13 +08:00
|
|
|
auto CurBSI = dyn_cast<BlockScopeInfo>(FunctionScopes.back());
|
|
|
|
if (CurBSI && CurBSI->TheDecl &&
|
|
|
|
!CurBSI->TheDecl->Encloses(CurContext)) {
|
|
|
|
// We have switched contexts due to template instantiation.
|
|
|
|
assert(!ActiveTemplateInstantiations.empty());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CurBSI;
|
Keep an explicit stack of function and block scopes, each element of
which has the label map, switch statement stack, etc. Previously, we
had a single set of maps in Sema (for the function) along with a stack
of block scopes. However, this lead to funky behavior with nested
functions, e.g., in the member functions of local classes.
The explicit-stack approach is far cleaner, and we retain a 1-element
cache so that we're not malloc/free'ing every time we enter a
function. Fixes PR6382.
Also, tweaked the unused-variable warning suppression logic to look at
errors within a given Scope rather than within a given function. The
prior code wasn't looking at the right number-of-errors count when
dealing with blocks, since the block's count would be deallocated
before we got to ActOnPopScope. This approach works with nested
blocks/functions, and gives tighter error recovery.
llvm-svn: 97518
2010-03-02 07:15:13 +08:00
|
|
|
}
|
2010-06-01 17:23:16 +08:00
|
|
|
|
2012-01-06 11:05:34 +08:00
|
|
|
LambdaScopeInfo *Sema::getCurLambda() {
|
|
|
|
if (FunctionScopes.empty())
|
|
|
|
return 0;
|
2013-01-20 09:04:14 +08:00
|
|
|
|
2014-04-27 02:29:13 +08:00
|
|
|
auto CurLSI = dyn_cast<LambdaScopeInfo>(FunctionScopes.back());
|
|
|
|
if (CurLSI && CurLSI->Lambda &&
|
|
|
|
!CurLSI->Lambda->Encloses(CurContext)) {
|
|
|
|
// We have switched contexts due to template instantiation.
|
|
|
|
assert(!ActiveTemplateInstantiations.empty());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CurLSI;
|
2012-01-06 11:05:34 +08:00
|
|
|
}
|
Implement a rudimentary form of generic lambdas.
Specifically, the following features are not included in this commit:
- any sort of capturing within generic lambdas
- generic lambdas within template functions and nested
within other generic lambdas
- conversion operator for captureless lambdas
- ensuring all visitors are generic lambda aware
(Although I have gotten some useful feedback on my patches of the above and will be incorporating that as I submit those patches for commit)
As an example of what compiles through this commit:
template <class F1, class F2>
struct overload : F1, F2 {
using F1::operator();
using F2::operator();
overload(F1 f1, F2 f2) : F1(f1), F2(f2) { }
};
auto Recursive = [](auto Self, auto h, auto ... rest) {
return 1 + Self(Self, rest...);
};
auto Base = [](auto Self, auto h) {
return 1;
};
overload<decltype(Base), decltype(Recursive)> O(Base, Recursive);
int num_params = O(O, 5, 3, "abc", 3.14, 'a');
Please see attached tests for more examples.
This patch has been reviewed by Doug and Richard. Minor changes (non-functionality affecting) have been made since both of them formally looked at it, but the changes involve removal of supernumerary return type deduction changes (since they are now redundant, with richard having committed a recent patch to address return type deduction for C++11 lambdas using C++14 semantics).
Some implementation notes:
- Add a new Declarator context => LambdaExprParameterContext to
clang::Declarator to allow the use of 'auto' in declaring generic
lambda parameters
- Add various helpers to CXXRecordDecl to facilitate identifying
and querying a closure class
- LambdaScopeInfo (which maintains the current lambda's Sema state)
was augmented to house the current depth of the template being
parsed (id est the Parser calls Sema::RecordParsingTemplateParameterDepth)
so that SemaType.cpp::ConvertDeclSpecToType may use it to immediately
generate a template-parameter-type when 'auto' is parsed in a generic
lambda parameter context. (i.e we do NOT use AutoType deduced to
a template parameter type - Richard seemed ok with this approach).
We encode that this template type was generated from an auto by simply
adding $auto to the name which can be used for better diagnostics if needed.
- SemaLambda.h was added to hold some common lambda utility
functions (this file is likely to grow ...)
- Teach Sema::ActOnStartOfFunctionDef to check whether it
is being called to instantiate a generic lambda's call
operator, and if so, push an appropriately prepared
LambdaScopeInfo object on the stack.
- various tests were added - but much more will be needed.
There is obviously more work to be done, and both Richard (weakly) and Doug (strongly)
have requested that LambdaExpr be removed form the CXXRecordDecl LambdaDefinitionaData
in a future patch which is forthcoming.
A greatful thanks to all reviewers including Eli Friedman, James Dennett,
and especially the two gracious wizards (Richard Smith and Doug Gregor)
who spent hours providing feedback (in person in Chicago and on the mailing lists).
And yet I am certain that I have allowed unidentified bugs to creep in; bugs, that I will do my best to slay, once identified!
Thanks!
llvm-svn: 191453
2013-09-27 03:54:12 +08:00
|
|
|
// We have a generic lambda if we parsed auto parameters, or we have
|
|
|
|
// an associated template parameter list.
|
|
|
|
LambdaScopeInfo *Sema::getCurGenericLambda() {
|
|
|
|
if (LambdaScopeInfo *LSI = getCurLambda()) {
|
|
|
|
return (LSI->AutoTemplateParams.size() ||
|
|
|
|
LSI->GLTemplateParameterList) ? LSI : 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-06 11:05:34 +08:00
|
|
|
|
2012-06-20 08:34:58 +08:00
|
|
|
void Sema::ActOnComment(SourceRange Comment) {
|
2012-09-13 14:41:18 +08:00
|
|
|
if (!LangOpts.RetainCommentsFromSystemHeaders &&
|
|
|
|
SourceMgr.isInSystemHeader(Comment.getBegin()))
|
|
|
|
return;
|
2013-04-10 23:35:17 +08:00
|
|
|
RawComment RC(SourceMgr, Comment, false,
|
|
|
|
LangOpts.CommentOpts.ParseAllComments);
|
2012-06-23 00:02:55 +08:00
|
|
|
if (RC.isAlmostTrailingComment()) {
|
|
|
|
SourceRange MagicMarkerRange(Comment.getBegin(),
|
|
|
|
Comment.getBegin().getLocWithOffset(3));
|
|
|
|
StringRef MagicMarkerText;
|
|
|
|
switch (RC.getKind()) {
|
2012-07-04 15:30:26 +08:00
|
|
|
case RawComment::RCK_OrdinaryBCPL:
|
2012-06-23 00:02:55 +08:00
|
|
|
MagicMarkerText = "///<";
|
|
|
|
break;
|
2012-07-04 15:30:26 +08:00
|
|
|
case RawComment::RCK_OrdinaryC:
|
2012-06-23 00:02:55 +08:00
|
|
|
MagicMarkerText = "/**<";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("if this is an almost Doxygen comment, "
|
|
|
|
"it should be ordinary");
|
|
|
|
}
|
|
|
|
Diag(Comment.getBegin(), diag::warn_not_a_doxygen_trailing_member_comment) <<
|
|
|
|
FixItHint::CreateReplacement(MagicMarkerRange, MagicMarkerText);
|
|
|
|
}
|
2012-06-20 08:34:58 +08:00
|
|
|
Context.addComment(RC);
|
|
|
|
}
|
|
|
|
|
2010-06-01 17:23:16 +08:00
|
|
|
// Pin this vtable to this file.
|
|
|
|
ExternalSemaSource::~ExternalSemaSource() {}
|
2010-08-27 07:41:50 +08:00
|
|
|
|
2012-01-25 08:49:42 +08:00
|
|
|
void ExternalSemaSource::ReadMethodPool(Selector Sel) { }
|
2010-09-29 04:23:00 +08:00
|
|
|
|
2011-06-29 00:20:02 +08:00
|
|
|
void ExternalSemaSource::ReadKnownNamespaces(
|
2013-01-20 09:04:14 +08:00
|
|
|
SmallVectorImpl<NamespaceDecl *> &Namespaces) {
|
2011-06-29 00:20:02 +08:00
|
|
|
}
|
|
|
|
|
2013-02-01 16:13:20 +08:00
|
|
|
void ExternalSemaSource::ReadUndefinedButUsed(
|
2013-01-31 11:23:57 +08:00
|
|
|
llvm::DenseMap<NamedDecl *, SourceLocation> &Undefined) {
|
2013-01-26 08:35:08 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void PrettyDeclStackTraceEntry::print(raw_ostream &OS) const {
|
2010-08-27 07:41:50 +08:00
|
|
|
SourceLocation Loc = this->Loc;
|
|
|
|
if (!Loc.isValid() && TheDecl) Loc = TheDecl->getLocation();
|
|
|
|
if (Loc.isValid()) {
|
|
|
|
Loc.print(OS, S.getSourceManager());
|
|
|
|
OS << ": ";
|
|
|
|
}
|
|
|
|
OS << Message;
|
|
|
|
|
|
|
|
if (TheDecl && isa<NamedDecl>(TheDecl)) {
|
|
|
|
std::string Name = cast<NamedDecl>(TheDecl)->getNameAsString();
|
|
|
|
if (!Name.empty())
|
|
|
|
OS << " '" << Name << '\'';
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << '\n';
|
|
|
|
}
|
2011-05-05 06:10:40 +08:00
|
|
|
|
|
|
|
/// \brief Figure out if an expression could be turned into a call.
|
|
|
|
///
|
|
|
|
/// Use this when trying to recover from an error where the programmer may have
|
|
|
|
/// written just the name of a function instead of actually calling it.
|
|
|
|
///
|
|
|
|
/// \param E - The expression to examine.
|
|
|
|
/// \param ZeroArgCallReturnTy - If the expression can be turned into a call
|
|
|
|
/// with no arguments, this parameter is set to the type returned by such a
|
|
|
|
/// call; otherwise, it is set to an empty QualType.
|
2011-10-12 07:14:30 +08:00
|
|
|
/// \param OverloadSet - If the expression is an overloaded function
|
2011-05-05 06:10:40 +08:00
|
|
|
/// name, this parameter is populated with the decls of the various overloads.
|
2013-06-22 07:54:45 +08:00
|
|
|
bool Sema::tryExprAsCall(Expr &E, QualType &ZeroArgCallReturnTy,
|
|
|
|
UnresolvedSetImpl &OverloadSet) {
|
2011-05-05 06:10:40 +08:00
|
|
|
ZeroArgCallReturnTy = QualType();
|
2011-10-12 07:14:30 +08:00
|
|
|
OverloadSet.clear();
|
|
|
|
|
2013-06-04 08:28:46 +08:00
|
|
|
const OverloadExpr *Overloads = NULL;
|
2013-06-22 07:54:45 +08:00
|
|
|
bool IsMemExpr = false;
|
2011-10-12 07:14:30 +08:00
|
|
|
if (E.getType() == Context.OverloadTy) {
|
|
|
|
OverloadExpr::FindResult FR = OverloadExpr::find(const_cast<Expr*>(&E));
|
|
|
|
|
2013-06-04 08:28:46 +08:00
|
|
|
// Ignore overloads that are pointer-to-member constants.
|
|
|
|
if (FR.HasFormOfMemberPointer)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Overloads = FR.Expression;
|
|
|
|
} else if (E.getType() == Context.BoundMemberTy) {
|
|
|
|
Overloads = dyn_cast<UnresolvedMemberExpr>(E.IgnoreParens());
|
2013-06-22 07:54:45 +08:00
|
|
|
IsMemExpr = true;
|
2013-06-04 08:28:46 +08:00
|
|
|
}
|
2013-06-22 07:54:45 +08:00
|
|
|
|
|
|
|
bool Ambiguous = false;
|
|
|
|
|
2013-06-04 08:28:46 +08:00
|
|
|
if (Overloads) {
|
2011-05-05 06:10:40 +08:00
|
|
|
for (OverloadExpr::decls_iterator it = Overloads->decls_begin(),
|
|
|
|
DeclsEnd = Overloads->decls_end(); it != DeclsEnd; ++it) {
|
2011-10-12 07:14:30 +08:00
|
|
|
OverloadSet.addDecl(*it);
|
|
|
|
|
2013-06-22 07:54:45 +08:00
|
|
|
// Check whether the function is a non-template, non-member which takes no
|
2011-10-12 07:14:30 +08:00
|
|
|
// arguments.
|
2013-06-22 07:54:45 +08:00
|
|
|
if (IsMemExpr)
|
|
|
|
continue;
|
2011-10-12 07:14:30 +08:00
|
|
|
if (const FunctionDecl *OverloadDecl
|
|
|
|
= dyn_cast<FunctionDecl>((*it)->getUnderlyingDecl())) {
|
2013-06-04 08:28:46 +08:00
|
|
|
if (OverloadDecl->getMinRequiredArguments() == 0) {
|
|
|
|
if (!ZeroArgCallReturnTy.isNull() && !Ambiguous) {
|
|
|
|
ZeroArgCallReturnTy = QualType();
|
|
|
|
Ambiguous = true;
|
|
|
|
} else
|
2014-01-26 00:55:45 +08:00
|
|
|
ZeroArgCallReturnTy = OverloadDecl->getReturnType();
|
2013-06-04 08:28:46 +08:00
|
|
|
}
|
2011-05-05 06:10:40 +08:00
|
|
|
}
|
|
|
|
}
|
2011-10-12 07:14:30 +08:00
|
|
|
|
2013-06-22 07:54:45 +08:00
|
|
|
// If it's not a member, use better machinery to try to resolve the call
|
|
|
|
if (!IsMemExpr)
|
|
|
|
return !ZeroArgCallReturnTy.isNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to call the member with no arguments - this will correctly handle
|
|
|
|
// member templates with defaults/deduction of template arguments, overloads
|
|
|
|
// with default arguments, etc.
|
2013-07-09 07:35:04 +08:00
|
|
|
if (IsMemExpr && !E.isTypeDependent()) {
|
2013-06-22 07:54:45 +08:00
|
|
|
bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
|
|
|
|
getDiagnostics().setSuppressAllDiagnostics(true);
|
|
|
|
ExprResult R = BuildCallToMemberFunction(NULL, &E, SourceLocation(), None,
|
|
|
|
SourceLocation());
|
|
|
|
getDiagnostics().setSuppressAllDiagnostics(Suppress);
|
|
|
|
if (R.isUsable()) {
|
|
|
|
ZeroArgCallReturnTy = R.get()->getType();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2011-05-05 06:10:40 +08:00
|
|
|
}
|
|
|
|
|
2011-10-12 07:14:30 +08:00
|
|
|
if (const DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E.IgnoreParens())) {
|
2011-05-05 06:10:40 +08:00
|
|
|
if (const FunctionDecl *Fun = dyn_cast<FunctionDecl>(DeclRef->getDecl())) {
|
|
|
|
if (Fun->getMinRequiredArguments() == 0)
|
2014-01-26 00:55:45 +08:00
|
|
|
ZeroArgCallReturnTy = Fun->getReturnType();
|
2011-05-05 06:10:40 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't have an expression that's convenient to get a FunctionDecl from,
|
|
|
|
// but we can at least check if the type is "function of 0 arguments".
|
|
|
|
QualType ExprTy = E.getType();
|
|
|
|
const FunctionType *FunTy = NULL;
|
2011-05-05 08:59:35 +08:00
|
|
|
QualType PointeeTy = ExprTy->getPointeeType();
|
|
|
|
if (!PointeeTy.isNull())
|
|
|
|
FunTy = PointeeTy->getAs<FunctionType>();
|
2011-05-05 06:10:40 +08:00
|
|
|
if (!FunTy)
|
|
|
|
FunTy = ExprTy->getAs<FunctionType>();
|
|
|
|
|
|
|
|
if (const FunctionProtoType *FPT =
|
|
|
|
dyn_cast_or_null<FunctionProtoType>(FunTy)) {
|
2014-01-21 04:26:09 +08:00
|
|
|
if (FPT->getNumParams() == 0)
|
2014-01-26 00:55:45 +08:00
|
|
|
ZeroArgCallReturnTy = FunTy->getReturnType();
|
2011-05-05 06:10:40 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Give notes for a set of overloads.
|
|
|
|
///
|
2013-06-22 07:54:45 +08:00
|
|
|
/// A companion to tryExprAsCall. In cases when the name that the programmer
|
2011-05-05 06:10:40 +08:00
|
|
|
/// wrote was an overloaded function, we may be able to make some guesses about
|
|
|
|
/// plausible overloads based on their return types; such guesses can be handed
|
|
|
|
/// off to this method to be emitted as notes.
|
|
|
|
///
|
|
|
|
/// \param Overloads - The overloads to note.
|
|
|
|
/// \param FinalNoteLoc - If we've suppressed printing some overloads due to
|
|
|
|
/// -fshow-overloads=best, this is the location to attach to the note about too
|
|
|
|
/// many candidates. Typically this will be the location of the original
|
|
|
|
/// ill-formed expression.
|
2011-10-12 07:14:30 +08:00
|
|
|
static void noteOverloads(Sema &S, const UnresolvedSetImpl &Overloads,
|
|
|
|
const SourceLocation FinalNoteLoc) {
|
2011-05-05 06:10:40 +08:00
|
|
|
int ShownOverloads = 0;
|
|
|
|
int SuppressedOverloads = 0;
|
|
|
|
for (UnresolvedSetImpl::iterator It = Overloads.begin(),
|
|
|
|
DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) {
|
|
|
|
// FIXME: Magic number for max shown overloads stolen from
|
|
|
|
// OverloadCandidateSet::NoteCandidates.
|
2012-10-24 07:11:23 +08:00
|
|
|
if (ShownOverloads >= 4 && S.Diags.getShowOverloads() == Ovl_Best) {
|
2011-05-05 06:10:40 +08:00
|
|
|
++SuppressedOverloads;
|
|
|
|
continue;
|
|
|
|
}
|
2011-10-12 07:14:30 +08:00
|
|
|
|
|
|
|
NamedDecl *Fn = (*It)->getUnderlyingDecl();
|
2011-11-16 05:43:28 +08:00
|
|
|
S.Diag(Fn->getLocation(), diag::note_possible_target_of_call);
|
2011-05-05 06:10:40 +08:00
|
|
|
++ShownOverloads;
|
|
|
|
}
|
2011-10-12 07:14:30 +08:00
|
|
|
|
2011-05-05 06:10:40 +08:00
|
|
|
if (SuppressedOverloads)
|
2011-10-12 07:14:30 +08:00
|
|
|
S.Diag(FinalNoteLoc, diag::note_ovl_too_many_candidates)
|
|
|
|
<< SuppressedOverloads;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void notePlausibleOverloads(Sema &S, SourceLocation Loc,
|
|
|
|
const UnresolvedSetImpl &Overloads,
|
|
|
|
bool (*IsPlausibleResult)(QualType)) {
|
|
|
|
if (!IsPlausibleResult)
|
|
|
|
return noteOverloads(S, Overloads, Loc);
|
|
|
|
|
|
|
|
UnresolvedSet<2> PlausibleOverloads;
|
|
|
|
for (OverloadExpr::decls_iterator It = Overloads.begin(),
|
|
|
|
DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) {
|
|
|
|
const FunctionDecl *OverloadDecl = cast<FunctionDecl>(*It);
|
2014-01-26 00:55:45 +08:00
|
|
|
QualType OverloadResultTy = OverloadDecl->getReturnType();
|
2011-10-12 07:14:30 +08:00
|
|
|
if (IsPlausibleResult(OverloadResultTy))
|
|
|
|
PlausibleOverloads.addDecl(It.getDecl());
|
|
|
|
}
|
|
|
|
noteOverloads(S, PlausibleOverloads, Loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Determine whether the given expression can be called by just
|
|
|
|
/// putting parentheses after it. Notably, expressions with unary
|
|
|
|
/// operators can't be because the unary operator will start parsing
|
|
|
|
/// outside the call.
|
|
|
|
static bool IsCallableWithAppend(Expr *E) {
|
|
|
|
E = E->IgnoreImplicit();
|
|
|
|
return (!isa<CStyleCastExpr>(E) &&
|
|
|
|
!isa<UnaryOperator>(E) &&
|
|
|
|
!isa<BinaryOperator>(E) &&
|
|
|
|
!isa<CXXOperatorCallExpr>(E));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sema::tryToRecoverWithCall(ExprResult &E, const PartialDiagnostic &PD,
|
|
|
|
bool ForceComplain,
|
|
|
|
bool (*IsPlausibleResult)(QualType)) {
|
|
|
|
SourceLocation Loc = E.get()->getExprLoc();
|
|
|
|
SourceRange Range = E.get()->getSourceRange();
|
|
|
|
|
|
|
|
QualType ZeroArgCallTy;
|
|
|
|
UnresolvedSet<4> Overloads;
|
2013-06-22 07:54:45 +08:00
|
|
|
if (tryExprAsCall(*E.get(), ZeroArgCallTy, Overloads) &&
|
2011-10-12 07:14:30 +08:00
|
|
|
!ZeroArgCallTy.isNull() &&
|
|
|
|
(!IsPlausibleResult || IsPlausibleResult(ZeroArgCallTy))) {
|
|
|
|
// At this point, we know E is potentially callable with 0
|
|
|
|
// arguments and that it returns something of a reasonable type,
|
|
|
|
// so we can emit a fixit and carry on pretending that E was
|
|
|
|
// actually a CallExpr.
|
2013-08-22 03:09:44 +08:00
|
|
|
SourceLocation ParenInsertionLoc = PP.getLocForEndOfToken(Range.getEnd());
|
2013-01-20 09:04:14 +08:00
|
|
|
Diag(Loc, PD)
|
2011-10-12 07:14:30 +08:00
|
|
|
<< /*zero-arg*/ 1 << Range
|
|
|
|
<< (IsCallableWithAppend(E.get())
|
|
|
|
? FixItHint::CreateInsertion(ParenInsertionLoc, "()")
|
|
|
|
: FixItHint());
|
|
|
|
notePlausibleOverloads(*this, Loc, Overloads, IsPlausibleResult);
|
|
|
|
|
|
|
|
// FIXME: Try this before emitting the fixit, and suppress diagnostics
|
|
|
|
// while doing so.
|
2013-08-22 03:09:44 +08:00
|
|
|
E = ActOnCallExpr(0, E.take(), Range.getEnd(), None,
|
|
|
|
Range.getEnd().getLocWithOffset(1));
|
2011-10-12 07:14:30 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ForceComplain) return false;
|
|
|
|
|
|
|
|
Diag(Loc, PD) << /*not zero-arg*/ 0 << Range;
|
|
|
|
notePlausibleOverloads(*this, Loc, Overloads, IsPlausibleResult);
|
|
|
|
E = ExprError();
|
|
|
|
return true;
|
2011-05-05 06:10:40 +08:00
|
|
|
}
|
2013-03-15 06:56:43 +08:00
|
|
|
|
|
|
|
IdentifierInfo *Sema::getSuperIdentifier() const {
|
|
|
|
if (!Ident_super)
|
|
|
|
Ident_super = &Context.Idents.get("super");
|
|
|
|
return Ident_super;
|
|
|
|
}
|
2013-04-17 03:37:38 +08:00
|
|
|
|
2013-06-21 05:44:55 +08:00
|
|
|
IdentifierInfo *Sema::getFloat128Identifier() const {
|
|
|
|
if (!Ident___float128)
|
|
|
|
Ident___float128 = &Context.Idents.get("__float128");
|
|
|
|
return Ident___float128;
|
|
|
|
}
|
|
|
|
|
2013-04-17 03:37:38 +08:00
|
|
|
void Sema::PushCapturedRegionScope(Scope *S, CapturedDecl *CD, RecordDecl *RD,
|
2013-04-29 21:32:41 +08:00
|
|
|
CapturedRegionKind K) {
|
2013-05-04 03:00:33 +08:00
|
|
|
CapturingScopeInfo *CSI = new CapturedRegionScopeInfo(getDiagnostics(), S, CD, RD,
|
|
|
|
CD->getContextParam(), K);
|
2013-04-17 03:37:38 +08:00
|
|
|
CSI->ReturnType = Context.VoidTy;
|
|
|
|
FunctionScopes.push_back(CSI);
|
|
|
|
}
|
|
|
|
|
|
|
|
CapturedRegionScopeInfo *Sema::getCurCapturedRegion() {
|
|
|
|
if (FunctionScopes.empty())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return dyn_cast<CapturedRegionScopeInfo>(FunctionScopes.back());
|
|
|
|
}
|