1f4a2713aSLionel Sambuc //===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements the actions class which performs semantic analysis and
11f4a2713aSLionel Sambuc // builds an AST out of a parse stream.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14f4a2713aSLionel Sambuc
15f4a2713aSLionel Sambuc #include "clang/Sema/SemaInternal.h"
16f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
17f4a2713aSLionel Sambuc #include "clang/AST/ASTDiagnostic.h"
18f4a2713aSLionel Sambuc #include "clang/AST/DeclCXX.h"
19f4a2713aSLionel Sambuc #include "clang/AST/DeclFriend.h"
20f4a2713aSLionel Sambuc #include "clang/AST/DeclObjC.h"
21f4a2713aSLionel Sambuc #include "clang/AST/Expr.h"
22f4a2713aSLionel Sambuc #include "clang/AST/ExprCXX.h"
23f4a2713aSLionel Sambuc #include "clang/AST/StmtCXX.h"
24*0a6a1f1dSLionel Sambuc #include "clang/Basic/DiagnosticOptions.h"
25f4a2713aSLionel Sambuc #include "clang/Basic/FileManager.h"
26f4a2713aSLionel Sambuc #include "clang/Basic/PartialDiagnostic.h"
27f4a2713aSLionel Sambuc #include "clang/Basic/TargetInfo.h"
28f4a2713aSLionel Sambuc #include "clang/Lex/HeaderSearch.h"
29f4a2713aSLionel Sambuc #include "clang/Lex/Preprocessor.h"
30f4a2713aSLionel Sambuc #include "clang/Sema/CXXFieldCollector.h"
31f4a2713aSLionel Sambuc #include "clang/Sema/DelayedDiagnostic.h"
32f4a2713aSLionel Sambuc #include "clang/Sema/ExternalSemaSource.h"
33f4a2713aSLionel Sambuc #include "clang/Sema/MultiplexExternalSemaSource.h"
34f4a2713aSLionel Sambuc #include "clang/Sema/ObjCMethodList.h"
35f4a2713aSLionel Sambuc #include "clang/Sema/PrettyDeclStackTrace.h"
36f4a2713aSLionel Sambuc #include "clang/Sema/Scope.h"
37f4a2713aSLionel Sambuc #include "clang/Sema/ScopeInfo.h"
38f4a2713aSLionel Sambuc #include "clang/Sema/SemaConsumer.h"
39f4a2713aSLionel Sambuc #include "clang/Sema/TemplateDeduction.h"
40f4a2713aSLionel Sambuc #include "llvm/ADT/APFloat.h"
41f4a2713aSLionel Sambuc #include "llvm/ADT/DenseMap.h"
42f4a2713aSLionel Sambuc #include "llvm/ADT/SmallSet.h"
43f4a2713aSLionel Sambuc #include "llvm/Support/CrashRecoveryContext.h"
44f4a2713aSLionel Sambuc using namespace clang;
45f4a2713aSLionel Sambuc using namespace sema;
46f4a2713aSLionel Sambuc
getLocForEndOfToken(SourceLocation Loc,unsigned Offset)47*0a6a1f1dSLionel Sambuc SourceLocation Sema::getLocForEndOfToken(SourceLocation Loc, unsigned Offset) {
48*0a6a1f1dSLionel Sambuc return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, LangOpts);
49*0a6a1f1dSLionel Sambuc }
50*0a6a1f1dSLionel Sambuc
getModuleLoader() const51*0a6a1f1dSLionel Sambuc ModuleLoader &Sema::getModuleLoader() const { return PP.getModuleLoader(); }
52*0a6a1f1dSLionel Sambuc
getPrintingPolicy(const ASTContext & Context,const Preprocessor & PP)53f4a2713aSLionel Sambuc PrintingPolicy Sema::getPrintingPolicy(const ASTContext &Context,
54f4a2713aSLionel Sambuc const Preprocessor &PP) {
55f4a2713aSLionel Sambuc PrintingPolicy Policy = Context.getPrintingPolicy();
56f4a2713aSLionel Sambuc Policy.Bool = Context.getLangOpts().Bool;
57f4a2713aSLionel Sambuc if (!Policy.Bool) {
58f4a2713aSLionel Sambuc if (const MacroInfo *
59f4a2713aSLionel Sambuc BoolMacro = PP.getMacroInfo(&Context.Idents.get("bool"))) {
60f4a2713aSLionel Sambuc Policy.Bool = BoolMacro->isObjectLike() &&
61f4a2713aSLionel Sambuc BoolMacro->getNumTokens() == 1 &&
62f4a2713aSLionel Sambuc BoolMacro->getReplacementToken(0).is(tok::kw__Bool);
63f4a2713aSLionel Sambuc }
64f4a2713aSLionel Sambuc }
65f4a2713aSLionel Sambuc
66f4a2713aSLionel Sambuc return Policy;
67f4a2713aSLionel Sambuc }
68f4a2713aSLionel Sambuc
ActOnTranslationUnitScope(Scope * S)69f4a2713aSLionel Sambuc void Sema::ActOnTranslationUnitScope(Scope *S) {
70f4a2713aSLionel Sambuc TUScope = S;
71f4a2713aSLionel Sambuc PushDeclContext(S, Context.getTranslationUnitDecl());
72f4a2713aSLionel Sambuc }
73f4a2713aSLionel Sambuc
Sema(Preprocessor & pp,ASTContext & ctxt,ASTConsumer & consumer,TranslationUnitKind TUKind,CodeCompleteConsumer * CodeCompleter)74f4a2713aSLionel Sambuc Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
75f4a2713aSLionel Sambuc TranslationUnitKind TUKind,
76f4a2713aSLionel Sambuc CodeCompleteConsumer *CodeCompleter)
77*0a6a1f1dSLionel Sambuc : ExternalSource(nullptr),
78f4a2713aSLionel Sambuc isMultiplexExternalSource(false), FPFeatures(pp.getLangOpts()),
79f4a2713aSLionel Sambuc LangOpts(pp.getLangOpts()), PP(pp), Context(ctxt), Consumer(consumer),
80f4a2713aSLionel Sambuc Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
81f4a2713aSLionel Sambuc CollectStats(false), CodeCompleter(CodeCompleter),
82*0a6a1f1dSLionel Sambuc CurContext(nullptr), OriginalLexicalContext(nullptr),
83*0a6a1f1dSLionel Sambuc PackContext(nullptr), MSStructPragmaOn(false),
84*0a6a1f1dSLionel Sambuc MSPointerToMemberRepresentationMethod(
85*0a6a1f1dSLionel Sambuc LangOpts.getMSPointerToMemberRepresentationMethod()),
86*0a6a1f1dSLionel Sambuc VtorDispModeStack(1, MSVtorDispAttr::Mode(LangOpts.VtorDispMode)),
87*0a6a1f1dSLionel Sambuc DataSegStack(nullptr), BSSSegStack(nullptr), ConstSegStack(nullptr),
88*0a6a1f1dSLionel Sambuc CodeSegStack(nullptr), CurInitSeg(nullptr), VisContext(nullptr),
89f4a2713aSLionel Sambuc IsBuildingRecoveryCallExpr(false),
90*0a6a1f1dSLionel Sambuc ExprNeedsCleanups(false), LateTemplateParser(nullptr),
91*0a6a1f1dSLionel Sambuc LateTemplateParserCleanup(nullptr),
92*0a6a1f1dSLionel Sambuc OpaqueParser(nullptr), IdResolver(pp), StdInitializerList(nullptr),
93*0a6a1f1dSLionel Sambuc CXXTypeInfoDecl(nullptr), MSVCGuidDecl(nullptr),
94*0a6a1f1dSLionel Sambuc NSNumberDecl(nullptr),
95*0a6a1f1dSLionel Sambuc NSStringDecl(nullptr), StringWithUTF8StringMethod(nullptr),
96*0a6a1f1dSLionel Sambuc NSArrayDecl(nullptr), ArrayWithObjectsMethod(nullptr),
97*0a6a1f1dSLionel Sambuc NSDictionaryDecl(nullptr), DictionaryWithObjectsMethod(nullptr),
98*0a6a1f1dSLionel Sambuc MSAsmLabelNameCounter(0),
99f4a2713aSLionel Sambuc GlobalNewDeleteDeclared(false),
100f4a2713aSLionel Sambuc TUKind(TUKind),
101*0a6a1f1dSLionel Sambuc NumSFINAEErrors(0),
102f4a2713aSLionel Sambuc AccessCheckingSFINAE(false), InNonInstantiationSFINAEContext(false),
103f4a2713aSLionel Sambuc NonInstantiationEntries(0), ArgumentPackSubstitutionIndex(-1),
104*0a6a1f1dSLionel Sambuc CurrentInstantiationScope(nullptr), DisableTypoCorrection(false),
105f4a2713aSLionel Sambuc TyposCorrected(0), AnalysisWarnings(*this),
106*0a6a1f1dSLionel Sambuc VarDataSharingAttributesStack(nullptr), CurScope(nullptr),
107*0a6a1f1dSLionel Sambuc Ident_super(nullptr), Ident___float128(nullptr)
108f4a2713aSLionel Sambuc {
109*0a6a1f1dSLionel Sambuc TUScope = nullptr;
110f4a2713aSLionel Sambuc
111f4a2713aSLionel Sambuc LoadedExternalKnownNamespaces = false;
112f4a2713aSLionel Sambuc for (unsigned I = 0; I != NSAPI::NumNSNumberLiteralMethods; ++I)
113*0a6a1f1dSLionel Sambuc NSNumberLiteralMethods[I] = nullptr;
114f4a2713aSLionel Sambuc
115f4a2713aSLionel Sambuc if (getLangOpts().ObjC1)
116f4a2713aSLionel Sambuc NSAPIObj.reset(new NSAPI(Context));
117f4a2713aSLionel Sambuc
118f4a2713aSLionel Sambuc if (getLangOpts().CPlusPlus)
119f4a2713aSLionel Sambuc FieldCollector.reset(new CXXFieldCollector());
120f4a2713aSLionel Sambuc
121f4a2713aSLionel Sambuc // Tell diagnostics how to render things from the AST library.
122f4a2713aSLionel Sambuc PP.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
123f4a2713aSLionel Sambuc &Context);
124f4a2713aSLionel Sambuc
125f4a2713aSLionel Sambuc ExprEvalContexts.push_back(
126f4a2713aSLionel Sambuc ExpressionEvaluationContextRecord(PotentiallyEvaluated, 0,
127*0a6a1f1dSLionel Sambuc false, nullptr, false));
128f4a2713aSLionel Sambuc
129f4a2713aSLionel Sambuc FunctionScopes.push_back(new FunctionScopeInfo(Diags));
130f4a2713aSLionel Sambuc
131f4a2713aSLionel Sambuc // Initilization of data sharing attributes stack for OpenMP
132f4a2713aSLionel Sambuc InitDataSharingAttributesStack();
133f4a2713aSLionel Sambuc }
134f4a2713aSLionel Sambuc
addImplicitTypedef(StringRef Name,QualType T)135*0a6a1f1dSLionel Sambuc void Sema::addImplicitTypedef(StringRef Name, QualType T) {
136*0a6a1f1dSLionel Sambuc DeclarationName DN = &Context.Idents.get(Name);
137*0a6a1f1dSLionel Sambuc if (IdResolver.begin(DN) == IdResolver.end())
138*0a6a1f1dSLionel Sambuc PushOnScopeChains(Context.buildImplicitTypedef(T, Name), TUScope);
139*0a6a1f1dSLionel Sambuc }
140*0a6a1f1dSLionel Sambuc
Initialize()141f4a2713aSLionel Sambuc void Sema::Initialize() {
142f4a2713aSLionel Sambuc // Tell the AST consumer about this Sema object.
143f4a2713aSLionel Sambuc Consumer.Initialize(Context);
144f4a2713aSLionel Sambuc
145f4a2713aSLionel Sambuc // FIXME: Isn't this redundant with the initialization above?
146f4a2713aSLionel Sambuc if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer))
147f4a2713aSLionel Sambuc SC->InitializeSema(*this);
148f4a2713aSLionel Sambuc
149f4a2713aSLionel Sambuc // Tell the external Sema source about this Sema object.
150f4a2713aSLionel Sambuc if (ExternalSemaSource *ExternalSema
151f4a2713aSLionel Sambuc = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource()))
152f4a2713aSLionel Sambuc ExternalSema->InitializeSema(*this);
153f4a2713aSLionel Sambuc
154*0a6a1f1dSLionel Sambuc // This needs to happen after ExternalSemaSource::InitializeSema(this) or we
155*0a6a1f1dSLionel Sambuc // will not be able to merge any duplicate __va_list_tag decls correctly.
156*0a6a1f1dSLionel Sambuc VAListTagName = PP.getIdentifierInfo("__va_list_tag");
157*0a6a1f1dSLionel Sambuc
158f4a2713aSLionel Sambuc // Initialize predefined 128-bit integer types, if needed.
159*0a6a1f1dSLionel Sambuc if (Context.getTargetInfo().hasInt128Type()) {
160f4a2713aSLionel Sambuc // If either of the 128-bit integer types are unavailable to name lookup,
161f4a2713aSLionel Sambuc // define them now.
162f4a2713aSLionel Sambuc DeclarationName Int128 = &Context.Idents.get("__int128_t");
163f4a2713aSLionel Sambuc if (IdResolver.begin(Int128) == IdResolver.end())
164f4a2713aSLionel Sambuc PushOnScopeChains(Context.getInt128Decl(), TUScope);
165f4a2713aSLionel Sambuc
166f4a2713aSLionel Sambuc DeclarationName UInt128 = &Context.Idents.get("__uint128_t");
167f4a2713aSLionel Sambuc if (IdResolver.begin(UInt128) == IdResolver.end())
168f4a2713aSLionel Sambuc PushOnScopeChains(Context.getUInt128Decl(), TUScope);
169f4a2713aSLionel Sambuc }
170f4a2713aSLionel Sambuc
171f4a2713aSLionel Sambuc
172f4a2713aSLionel Sambuc // Initialize predefined Objective-C types:
173f4a2713aSLionel Sambuc if (PP.getLangOpts().ObjC1) {
174f4a2713aSLionel Sambuc // If 'SEL' does not yet refer to any declarations, make it refer to the
175f4a2713aSLionel Sambuc // predefined 'SEL'.
176f4a2713aSLionel Sambuc DeclarationName SEL = &Context.Idents.get("SEL");
177f4a2713aSLionel Sambuc if (IdResolver.begin(SEL) == IdResolver.end())
178f4a2713aSLionel Sambuc PushOnScopeChains(Context.getObjCSelDecl(), TUScope);
179f4a2713aSLionel Sambuc
180f4a2713aSLionel Sambuc // If 'id' does not yet refer to any declarations, make it refer to the
181f4a2713aSLionel Sambuc // predefined 'id'.
182f4a2713aSLionel Sambuc DeclarationName Id = &Context.Idents.get("id");
183f4a2713aSLionel Sambuc if (IdResolver.begin(Id) == IdResolver.end())
184f4a2713aSLionel Sambuc PushOnScopeChains(Context.getObjCIdDecl(), TUScope);
185f4a2713aSLionel Sambuc
186f4a2713aSLionel Sambuc // Create the built-in typedef for 'Class'.
187f4a2713aSLionel Sambuc DeclarationName Class = &Context.Idents.get("Class");
188f4a2713aSLionel Sambuc if (IdResolver.begin(Class) == IdResolver.end())
189f4a2713aSLionel Sambuc PushOnScopeChains(Context.getObjCClassDecl(), TUScope);
190f4a2713aSLionel Sambuc
191f4a2713aSLionel Sambuc // Create the built-in forward declaratino for 'Protocol'.
192f4a2713aSLionel Sambuc DeclarationName Protocol = &Context.Idents.get("Protocol");
193f4a2713aSLionel Sambuc if (IdResolver.begin(Protocol) == IdResolver.end())
194f4a2713aSLionel Sambuc PushOnScopeChains(Context.getObjCProtocolDecl(), TUScope);
195f4a2713aSLionel Sambuc }
196f4a2713aSLionel Sambuc
197*0a6a1f1dSLionel Sambuc // Initialize Microsoft "predefined C++ types".
198*0a6a1f1dSLionel Sambuc if (PP.getLangOpts().MSVCCompat && PP.getLangOpts().CPlusPlus) {
199*0a6a1f1dSLionel Sambuc if (IdResolver.begin(&Context.Idents.get("type_info")) == IdResolver.end())
200*0a6a1f1dSLionel Sambuc PushOnScopeChains(Context.buildImplicitRecord("type_info", TTK_Class),
201*0a6a1f1dSLionel Sambuc TUScope);
202*0a6a1f1dSLionel Sambuc
203*0a6a1f1dSLionel Sambuc addImplicitTypedef("size_t", Context.getSizeType());
204*0a6a1f1dSLionel Sambuc }
205*0a6a1f1dSLionel Sambuc
206*0a6a1f1dSLionel Sambuc // Initialize predefined OpenCL types.
207*0a6a1f1dSLionel Sambuc if (PP.getLangOpts().OpenCL) {
208*0a6a1f1dSLionel Sambuc addImplicitTypedef("image1d_t", Context.OCLImage1dTy);
209*0a6a1f1dSLionel Sambuc addImplicitTypedef("image1d_array_t", Context.OCLImage1dArrayTy);
210*0a6a1f1dSLionel Sambuc addImplicitTypedef("image1d_buffer_t", Context.OCLImage1dBufferTy);
211*0a6a1f1dSLionel Sambuc addImplicitTypedef("image2d_t", Context.OCLImage2dTy);
212*0a6a1f1dSLionel Sambuc addImplicitTypedef("image2d_array_t", Context.OCLImage2dArrayTy);
213*0a6a1f1dSLionel Sambuc addImplicitTypedef("image3d_t", Context.OCLImage3dTy);
214*0a6a1f1dSLionel Sambuc addImplicitTypedef("sampler_t", Context.OCLSamplerTy);
215*0a6a1f1dSLionel Sambuc addImplicitTypedef("event_t", Context.OCLEventTy);
216*0a6a1f1dSLionel Sambuc }
217*0a6a1f1dSLionel Sambuc
218f4a2713aSLionel Sambuc DeclarationName BuiltinVaList = &Context.Idents.get("__builtin_va_list");
219f4a2713aSLionel Sambuc if (IdResolver.begin(BuiltinVaList) == IdResolver.end())
220f4a2713aSLionel Sambuc PushOnScopeChains(Context.getBuiltinVaListDecl(), TUScope);
221f4a2713aSLionel Sambuc }
222f4a2713aSLionel Sambuc
~Sema()223f4a2713aSLionel Sambuc Sema::~Sema() {
224*0a6a1f1dSLionel Sambuc llvm::DeleteContainerSeconds(LateParsedTemplateMap);
225f4a2713aSLionel Sambuc if (PackContext) FreePackedContext();
226f4a2713aSLionel Sambuc if (VisContext) FreeVisContext();
227f4a2713aSLionel Sambuc // Kill all the active scopes.
228f4a2713aSLionel Sambuc for (unsigned I = 1, E = FunctionScopes.size(); I != E; ++I)
229f4a2713aSLionel Sambuc delete FunctionScopes[I];
230f4a2713aSLionel Sambuc if (FunctionScopes.size() == 1)
231f4a2713aSLionel Sambuc delete FunctionScopes[0];
232f4a2713aSLionel Sambuc
233f4a2713aSLionel Sambuc // Tell the SemaConsumer to forget about us; we're going out of scope.
234f4a2713aSLionel Sambuc if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer))
235f4a2713aSLionel Sambuc SC->ForgetSema();
236f4a2713aSLionel Sambuc
237f4a2713aSLionel Sambuc // Detach from the external Sema source.
238f4a2713aSLionel Sambuc if (ExternalSemaSource *ExternalSema
239f4a2713aSLionel Sambuc = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource()))
240f4a2713aSLionel Sambuc ExternalSema->ForgetSema();
241f4a2713aSLionel Sambuc
242f4a2713aSLionel Sambuc // If Sema's ExternalSource is the multiplexer - we own it.
243f4a2713aSLionel Sambuc if (isMultiplexExternalSource)
244f4a2713aSLionel Sambuc delete ExternalSource;
245f4a2713aSLionel Sambuc
246f4a2713aSLionel Sambuc // Destroys data sharing attributes stack for OpenMP
247f4a2713aSLionel Sambuc DestroyDataSharingAttributesStack();
248*0a6a1f1dSLionel Sambuc
249*0a6a1f1dSLionel Sambuc assert(DelayedTypos.empty() && "Uncorrected typos!");
250f4a2713aSLionel Sambuc }
251f4a2713aSLionel Sambuc
252f4a2713aSLionel Sambuc /// makeUnavailableInSystemHeader - There is an error in the current
253f4a2713aSLionel Sambuc /// context. If we're still in a system header, and we can plausibly
254f4a2713aSLionel Sambuc /// make the relevant declaration unavailable instead of erroring, do
255f4a2713aSLionel Sambuc /// so and return true.
makeUnavailableInSystemHeader(SourceLocation loc,StringRef msg)256f4a2713aSLionel Sambuc bool Sema::makeUnavailableInSystemHeader(SourceLocation loc,
257f4a2713aSLionel Sambuc StringRef msg) {
258f4a2713aSLionel Sambuc // If we're not in a function, it's an error.
259f4a2713aSLionel Sambuc FunctionDecl *fn = dyn_cast<FunctionDecl>(CurContext);
260f4a2713aSLionel Sambuc if (!fn) return false;
261f4a2713aSLionel Sambuc
262f4a2713aSLionel Sambuc // If we're in template instantiation, it's an error.
263f4a2713aSLionel Sambuc if (!ActiveTemplateInstantiations.empty())
264f4a2713aSLionel Sambuc return false;
265f4a2713aSLionel Sambuc
266f4a2713aSLionel Sambuc // If that function's not in a system header, it's an error.
267f4a2713aSLionel Sambuc if (!Context.getSourceManager().isInSystemHeader(loc))
268f4a2713aSLionel Sambuc return false;
269f4a2713aSLionel Sambuc
270f4a2713aSLionel Sambuc // If the function is already unavailable, it's not an error.
271f4a2713aSLionel Sambuc if (fn->hasAttr<UnavailableAttr>()) return true;
272f4a2713aSLionel Sambuc
273*0a6a1f1dSLionel Sambuc fn->addAttr(UnavailableAttr::CreateImplicit(Context, msg, loc));
274f4a2713aSLionel Sambuc return true;
275f4a2713aSLionel Sambuc }
276f4a2713aSLionel Sambuc
getASTMutationListener() const277f4a2713aSLionel Sambuc ASTMutationListener *Sema::getASTMutationListener() const {
278f4a2713aSLionel Sambuc return getASTConsumer().GetASTMutationListener();
279f4a2713aSLionel Sambuc }
280f4a2713aSLionel Sambuc
281f4a2713aSLionel Sambuc ///\brief Registers an external source. If an external source already exists,
282f4a2713aSLionel Sambuc /// creates a multiplex external source and appends to it.
283f4a2713aSLionel Sambuc ///
284f4a2713aSLionel Sambuc ///\param[in] E - A non-null external sema source.
285f4a2713aSLionel Sambuc ///
addExternalSource(ExternalSemaSource * E)286f4a2713aSLionel Sambuc void Sema::addExternalSource(ExternalSemaSource *E) {
287f4a2713aSLionel Sambuc assert(E && "Cannot use with NULL ptr");
288f4a2713aSLionel Sambuc
289f4a2713aSLionel Sambuc if (!ExternalSource) {
290f4a2713aSLionel Sambuc ExternalSource = E;
291f4a2713aSLionel Sambuc return;
292f4a2713aSLionel Sambuc }
293f4a2713aSLionel Sambuc
294f4a2713aSLionel Sambuc if (isMultiplexExternalSource)
295f4a2713aSLionel Sambuc static_cast<MultiplexExternalSemaSource*>(ExternalSource)->addSource(*E);
296f4a2713aSLionel Sambuc else {
297f4a2713aSLionel Sambuc ExternalSource = new MultiplexExternalSemaSource(*ExternalSource, *E);
298f4a2713aSLionel Sambuc isMultiplexExternalSource = true;
299f4a2713aSLionel Sambuc }
300f4a2713aSLionel Sambuc }
301f4a2713aSLionel Sambuc
302f4a2713aSLionel Sambuc /// \brief Print out statistics about the semantic analysis.
PrintStats() const303f4a2713aSLionel Sambuc void Sema::PrintStats() const {
304f4a2713aSLionel Sambuc llvm::errs() << "\n*** Semantic Analysis Stats:\n";
305f4a2713aSLionel Sambuc llvm::errs() << NumSFINAEErrors << " SFINAE diagnostics trapped.\n";
306f4a2713aSLionel Sambuc
307f4a2713aSLionel Sambuc BumpAlloc.PrintStats();
308f4a2713aSLionel Sambuc AnalysisWarnings.PrintStats();
309f4a2713aSLionel Sambuc }
310f4a2713aSLionel Sambuc
311f4a2713aSLionel Sambuc /// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
312f4a2713aSLionel Sambuc /// If there is already an implicit cast, merge into the existing one.
313f4a2713aSLionel Sambuc /// The result is of the given category.
ImpCastExprToType(Expr * E,QualType Ty,CastKind Kind,ExprValueKind VK,const CXXCastPath * BasePath,CheckedConversionKind CCK)314f4a2713aSLionel Sambuc ExprResult Sema::ImpCastExprToType(Expr *E, QualType Ty,
315f4a2713aSLionel Sambuc CastKind Kind, ExprValueKind VK,
316f4a2713aSLionel Sambuc const CXXCastPath *BasePath,
317f4a2713aSLionel Sambuc CheckedConversionKind CCK) {
318f4a2713aSLionel Sambuc #ifndef NDEBUG
319f4a2713aSLionel Sambuc if (VK == VK_RValue && !E->isRValue()) {
320f4a2713aSLionel Sambuc switch (Kind) {
321f4a2713aSLionel Sambuc default:
322*0a6a1f1dSLionel Sambuc llvm_unreachable("can't implicitly cast lvalue to rvalue with this cast "
323*0a6a1f1dSLionel Sambuc "kind");
324f4a2713aSLionel Sambuc case CK_LValueToRValue:
325f4a2713aSLionel Sambuc case CK_ArrayToPointerDecay:
326f4a2713aSLionel Sambuc case CK_FunctionToPointerDecay:
327f4a2713aSLionel Sambuc case CK_ToVoid:
328f4a2713aSLionel Sambuc break;
329f4a2713aSLionel Sambuc }
330f4a2713aSLionel Sambuc }
331f4a2713aSLionel Sambuc assert((VK == VK_RValue || !E->isRValue()) && "can't cast rvalue to lvalue");
332f4a2713aSLionel Sambuc #endif
333f4a2713aSLionel Sambuc
334f4a2713aSLionel Sambuc QualType ExprTy = Context.getCanonicalType(E->getType());
335f4a2713aSLionel Sambuc QualType TypeTy = Context.getCanonicalType(Ty);
336f4a2713aSLionel Sambuc
337f4a2713aSLionel Sambuc if (ExprTy == TypeTy)
338*0a6a1f1dSLionel Sambuc return E;
339f4a2713aSLionel Sambuc
340f4a2713aSLionel Sambuc // If this is a derived-to-base cast to a through a virtual base, we
341f4a2713aSLionel Sambuc // need a vtable.
342f4a2713aSLionel Sambuc if (Kind == CK_DerivedToBase &&
343f4a2713aSLionel Sambuc BasePathInvolvesVirtualBase(*BasePath)) {
344f4a2713aSLionel Sambuc QualType T = E->getType();
345f4a2713aSLionel Sambuc if (const PointerType *Pointer = T->getAs<PointerType>())
346f4a2713aSLionel Sambuc T = Pointer->getPointeeType();
347f4a2713aSLionel Sambuc if (const RecordType *RecordTy = T->getAs<RecordType>())
348f4a2713aSLionel Sambuc MarkVTableUsed(E->getLocStart(),
349f4a2713aSLionel Sambuc cast<CXXRecordDecl>(RecordTy->getDecl()));
350f4a2713aSLionel Sambuc }
351f4a2713aSLionel Sambuc
352f4a2713aSLionel Sambuc if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(E)) {
353f4a2713aSLionel Sambuc if (ImpCast->getCastKind() == Kind && (!BasePath || BasePath->empty())) {
354f4a2713aSLionel Sambuc ImpCast->setType(Ty);
355f4a2713aSLionel Sambuc ImpCast->setValueKind(VK);
356*0a6a1f1dSLionel Sambuc return E;
357f4a2713aSLionel Sambuc }
358f4a2713aSLionel Sambuc }
359f4a2713aSLionel Sambuc
360*0a6a1f1dSLionel Sambuc return ImplicitCastExpr::Create(Context, Ty, Kind, E, BasePath, VK);
361f4a2713aSLionel Sambuc }
362f4a2713aSLionel Sambuc
363f4a2713aSLionel Sambuc /// ScalarTypeToBooleanCastKind - Returns the cast kind corresponding
364f4a2713aSLionel Sambuc /// to the conversion from scalar type ScalarTy to the Boolean type.
ScalarTypeToBooleanCastKind(QualType ScalarTy)365f4a2713aSLionel Sambuc CastKind Sema::ScalarTypeToBooleanCastKind(QualType ScalarTy) {
366f4a2713aSLionel Sambuc switch (ScalarTy->getScalarTypeKind()) {
367f4a2713aSLionel Sambuc case Type::STK_Bool: return CK_NoOp;
368f4a2713aSLionel Sambuc case Type::STK_CPointer: return CK_PointerToBoolean;
369f4a2713aSLionel Sambuc case Type::STK_BlockPointer: return CK_PointerToBoolean;
370f4a2713aSLionel Sambuc case Type::STK_ObjCObjectPointer: return CK_PointerToBoolean;
371f4a2713aSLionel Sambuc case Type::STK_MemberPointer: return CK_MemberPointerToBoolean;
372f4a2713aSLionel Sambuc case Type::STK_Integral: return CK_IntegralToBoolean;
373f4a2713aSLionel Sambuc case Type::STK_Floating: return CK_FloatingToBoolean;
374f4a2713aSLionel Sambuc case Type::STK_IntegralComplex: return CK_IntegralComplexToBoolean;
375f4a2713aSLionel Sambuc case Type::STK_FloatingComplex: return CK_FloatingComplexToBoolean;
376f4a2713aSLionel Sambuc }
377f4a2713aSLionel Sambuc return CK_Invalid;
378f4a2713aSLionel Sambuc }
379f4a2713aSLionel Sambuc
380f4a2713aSLionel Sambuc /// \brief Used to prune the decls of Sema's UnusedFileScopedDecls vector.
ShouldRemoveFromUnused(Sema * SemaRef,const DeclaratorDecl * D)381f4a2713aSLionel Sambuc static bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) {
382f4a2713aSLionel Sambuc if (D->getMostRecentDecl()->isUsed())
383f4a2713aSLionel Sambuc return true;
384f4a2713aSLionel Sambuc
385f4a2713aSLionel Sambuc if (D->isExternallyVisible())
386f4a2713aSLionel Sambuc return true;
387f4a2713aSLionel Sambuc
388f4a2713aSLionel Sambuc if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
389f4a2713aSLionel Sambuc // UnusedFileScopedDecls stores the first declaration.
390f4a2713aSLionel Sambuc // The declaration may have become definition so check again.
391f4a2713aSLionel Sambuc const FunctionDecl *DeclToCheck;
392f4a2713aSLionel Sambuc if (FD->hasBody(DeclToCheck))
393f4a2713aSLionel Sambuc return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
394f4a2713aSLionel Sambuc
395f4a2713aSLionel Sambuc // Later redecls may add new information resulting in not having to warn,
396f4a2713aSLionel Sambuc // so check again.
397f4a2713aSLionel Sambuc DeclToCheck = FD->getMostRecentDecl();
398f4a2713aSLionel Sambuc if (DeclToCheck != FD)
399f4a2713aSLionel Sambuc return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
400f4a2713aSLionel Sambuc }
401f4a2713aSLionel Sambuc
402f4a2713aSLionel Sambuc if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
403f4a2713aSLionel Sambuc // If a variable usable in constant expressions is referenced,
404f4a2713aSLionel Sambuc // don't warn if it isn't used: if the value of a variable is required
405f4a2713aSLionel Sambuc // for the computation of a constant expression, it doesn't make sense to
406f4a2713aSLionel Sambuc // warn even if the variable isn't odr-used. (isReferenced doesn't
407f4a2713aSLionel Sambuc // precisely reflect that, but it's a decent approximation.)
408f4a2713aSLionel Sambuc if (VD->isReferenced() &&
409f4a2713aSLionel Sambuc VD->isUsableInConstantExpressions(SemaRef->Context))
410f4a2713aSLionel Sambuc return true;
411f4a2713aSLionel Sambuc
412f4a2713aSLionel Sambuc // UnusedFileScopedDecls stores the first declaration.
413f4a2713aSLionel Sambuc // The declaration may have become definition so check again.
414f4a2713aSLionel Sambuc const VarDecl *DeclToCheck = VD->getDefinition();
415f4a2713aSLionel Sambuc if (DeclToCheck)
416f4a2713aSLionel Sambuc return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
417f4a2713aSLionel Sambuc
418f4a2713aSLionel Sambuc // Later redecls may add new information resulting in not having to warn,
419f4a2713aSLionel Sambuc // so check again.
420f4a2713aSLionel Sambuc DeclToCheck = VD->getMostRecentDecl();
421f4a2713aSLionel Sambuc if (DeclToCheck != VD)
422f4a2713aSLionel Sambuc return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
423f4a2713aSLionel Sambuc }
424f4a2713aSLionel Sambuc
425f4a2713aSLionel Sambuc return false;
426f4a2713aSLionel Sambuc }
427f4a2713aSLionel Sambuc
428f4a2713aSLionel Sambuc /// Obtains a sorted list of functions that are undefined but ODR-used.
getUndefinedButUsed(SmallVectorImpl<std::pair<NamedDecl *,SourceLocation>> & Undefined)429f4a2713aSLionel Sambuc void Sema::getUndefinedButUsed(
430f4a2713aSLionel Sambuc SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> > &Undefined) {
431f4a2713aSLionel Sambuc for (llvm::DenseMap<NamedDecl *, SourceLocation>::iterator
432f4a2713aSLionel Sambuc I = UndefinedButUsed.begin(), E = UndefinedButUsed.end();
433f4a2713aSLionel Sambuc I != E; ++I) {
434f4a2713aSLionel Sambuc NamedDecl *ND = I->first;
435f4a2713aSLionel Sambuc
436f4a2713aSLionel Sambuc // Ignore attributes that have become invalid.
437f4a2713aSLionel Sambuc if (ND->isInvalidDecl()) continue;
438f4a2713aSLionel Sambuc
439f4a2713aSLionel Sambuc // __attribute__((weakref)) is basically a definition.
440f4a2713aSLionel Sambuc if (ND->hasAttr<WeakRefAttr>()) continue;
441f4a2713aSLionel Sambuc
442f4a2713aSLionel Sambuc if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
443f4a2713aSLionel Sambuc if (FD->isDefined())
444f4a2713aSLionel Sambuc continue;
445f4a2713aSLionel Sambuc if (FD->isExternallyVisible() &&
446f4a2713aSLionel Sambuc !FD->getMostRecentDecl()->isInlined())
447f4a2713aSLionel Sambuc continue;
448f4a2713aSLionel Sambuc } else {
449f4a2713aSLionel Sambuc if (cast<VarDecl>(ND)->hasDefinition() != VarDecl::DeclarationOnly)
450f4a2713aSLionel Sambuc continue;
451f4a2713aSLionel Sambuc if (ND->isExternallyVisible())
452f4a2713aSLionel Sambuc continue;
453f4a2713aSLionel Sambuc }
454f4a2713aSLionel Sambuc
455f4a2713aSLionel Sambuc Undefined.push_back(std::make_pair(ND, I->second));
456f4a2713aSLionel Sambuc }
457f4a2713aSLionel Sambuc
458f4a2713aSLionel Sambuc // Sort (in order of use site) so that we're not dependent on the iteration
459f4a2713aSLionel Sambuc // order through an llvm::DenseMap.
460*0a6a1f1dSLionel Sambuc SourceManager &SM = Context.getSourceManager();
461f4a2713aSLionel Sambuc std::sort(Undefined.begin(), Undefined.end(),
462*0a6a1f1dSLionel Sambuc [&SM](const std::pair<NamedDecl *, SourceLocation> &l,
463*0a6a1f1dSLionel Sambuc const std::pair<NamedDecl *, SourceLocation> &r) {
464*0a6a1f1dSLionel Sambuc if (l.second.isValid() && !r.second.isValid())
465*0a6a1f1dSLionel Sambuc return true;
466*0a6a1f1dSLionel Sambuc if (!l.second.isValid() && r.second.isValid())
467*0a6a1f1dSLionel Sambuc return false;
468*0a6a1f1dSLionel Sambuc if (l.second != r.second)
469*0a6a1f1dSLionel Sambuc return SM.isBeforeInTranslationUnit(l.second, r.second);
470*0a6a1f1dSLionel Sambuc return SM.isBeforeInTranslationUnit(l.first->getLocation(),
471*0a6a1f1dSLionel Sambuc r.first->getLocation());
472*0a6a1f1dSLionel Sambuc });
473f4a2713aSLionel Sambuc }
474f4a2713aSLionel Sambuc
475f4a2713aSLionel Sambuc /// checkUndefinedButUsed - Check for undefined objects with internal linkage
476f4a2713aSLionel Sambuc /// or that are inline.
checkUndefinedButUsed(Sema & S)477f4a2713aSLionel Sambuc static void checkUndefinedButUsed(Sema &S) {
478f4a2713aSLionel Sambuc if (S.UndefinedButUsed.empty()) return;
479f4a2713aSLionel Sambuc
480f4a2713aSLionel Sambuc // Collect all the still-undefined entities with internal linkage.
481f4a2713aSLionel Sambuc SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
482f4a2713aSLionel Sambuc S.getUndefinedButUsed(Undefined);
483f4a2713aSLionel Sambuc if (Undefined.empty()) return;
484f4a2713aSLionel Sambuc
485f4a2713aSLionel Sambuc for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
486f4a2713aSLionel Sambuc I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
487f4a2713aSLionel Sambuc NamedDecl *ND = I->first;
488f4a2713aSLionel Sambuc
489*0a6a1f1dSLionel Sambuc if (ND->hasAttr<DLLImportAttr>() || ND->hasAttr<DLLExportAttr>()) {
490*0a6a1f1dSLionel Sambuc // An exported function will always be emitted when defined, so even if
491*0a6a1f1dSLionel Sambuc // the function is inline, it doesn't have to be emitted in this TU. An
492*0a6a1f1dSLionel Sambuc // imported function implies that it has been exported somewhere else.
493*0a6a1f1dSLionel Sambuc continue;
494*0a6a1f1dSLionel Sambuc }
495*0a6a1f1dSLionel Sambuc
496f4a2713aSLionel Sambuc if (!ND->isExternallyVisible()) {
497f4a2713aSLionel Sambuc S.Diag(ND->getLocation(), diag::warn_undefined_internal)
498f4a2713aSLionel Sambuc << isa<VarDecl>(ND) << ND;
499f4a2713aSLionel Sambuc } else {
500f4a2713aSLionel Sambuc assert(cast<FunctionDecl>(ND)->getMostRecentDecl()->isInlined() &&
501f4a2713aSLionel Sambuc "used object requires definition but isn't inline or internal?");
502f4a2713aSLionel Sambuc S.Diag(ND->getLocation(), diag::warn_undefined_inline) << ND;
503f4a2713aSLionel Sambuc }
504f4a2713aSLionel Sambuc if (I->second.isValid())
505f4a2713aSLionel Sambuc S.Diag(I->second, diag::note_used_here);
506f4a2713aSLionel Sambuc }
507f4a2713aSLionel Sambuc }
508f4a2713aSLionel Sambuc
LoadExternalWeakUndeclaredIdentifiers()509f4a2713aSLionel Sambuc void Sema::LoadExternalWeakUndeclaredIdentifiers() {
510f4a2713aSLionel Sambuc if (!ExternalSource)
511f4a2713aSLionel Sambuc return;
512f4a2713aSLionel Sambuc
513f4a2713aSLionel Sambuc SmallVector<std::pair<IdentifierInfo *, WeakInfo>, 4> WeakIDs;
514f4a2713aSLionel Sambuc ExternalSource->ReadWeakUndeclaredIdentifiers(WeakIDs);
515f4a2713aSLionel Sambuc for (unsigned I = 0, N = WeakIDs.size(); I != N; ++I) {
516f4a2713aSLionel Sambuc llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator Pos
517f4a2713aSLionel Sambuc = WeakUndeclaredIdentifiers.find(WeakIDs[I].first);
518f4a2713aSLionel Sambuc if (Pos != WeakUndeclaredIdentifiers.end())
519f4a2713aSLionel Sambuc continue;
520f4a2713aSLionel Sambuc
521f4a2713aSLionel Sambuc WeakUndeclaredIdentifiers.insert(WeakIDs[I]);
522f4a2713aSLionel Sambuc }
523f4a2713aSLionel Sambuc }
524f4a2713aSLionel Sambuc
525f4a2713aSLionel Sambuc
526f4a2713aSLionel Sambuc typedef llvm::DenseMap<const CXXRecordDecl*, bool> RecordCompleteMap;
527f4a2713aSLionel Sambuc
528f4a2713aSLionel Sambuc /// \brief Returns true, if all methods and nested classes of the given
529f4a2713aSLionel Sambuc /// CXXRecordDecl are defined in this translation unit.
530f4a2713aSLionel Sambuc ///
531f4a2713aSLionel Sambuc /// Should only be called from ActOnEndOfTranslationUnit so that all
532f4a2713aSLionel Sambuc /// definitions are actually read.
MethodsAndNestedClassesComplete(const CXXRecordDecl * RD,RecordCompleteMap & MNCComplete)533f4a2713aSLionel Sambuc static bool MethodsAndNestedClassesComplete(const CXXRecordDecl *RD,
534f4a2713aSLionel Sambuc RecordCompleteMap &MNCComplete) {
535f4a2713aSLionel Sambuc RecordCompleteMap::iterator Cache = MNCComplete.find(RD);
536f4a2713aSLionel Sambuc if (Cache != MNCComplete.end())
537f4a2713aSLionel Sambuc return Cache->second;
538f4a2713aSLionel Sambuc if (!RD->isCompleteDefinition())
539f4a2713aSLionel Sambuc return false;
540f4a2713aSLionel Sambuc bool Complete = true;
541f4a2713aSLionel Sambuc for (DeclContext::decl_iterator I = RD->decls_begin(),
542f4a2713aSLionel Sambuc E = RD->decls_end();
543f4a2713aSLionel Sambuc I != E && Complete; ++I) {
544f4a2713aSLionel Sambuc if (const CXXMethodDecl *M = dyn_cast<CXXMethodDecl>(*I))
545f4a2713aSLionel Sambuc Complete = M->isDefined() || (M->isPure() && !isa<CXXDestructorDecl>(M));
546f4a2713aSLionel Sambuc else if (const FunctionTemplateDecl *F = dyn_cast<FunctionTemplateDecl>(*I))
547*0a6a1f1dSLionel Sambuc // If the template function is marked as late template parsed at this point,
548*0a6a1f1dSLionel Sambuc // it has not been instantiated and therefore we have not performed semantic
549*0a6a1f1dSLionel Sambuc // analysis on it yet, so we cannot know if the type can be considered
550*0a6a1f1dSLionel Sambuc // complete.
551*0a6a1f1dSLionel Sambuc Complete = !F->getTemplatedDecl()->isLateTemplateParsed() &&
552*0a6a1f1dSLionel Sambuc F->getTemplatedDecl()->isDefined();
553f4a2713aSLionel Sambuc else if (const CXXRecordDecl *R = dyn_cast<CXXRecordDecl>(*I)) {
554f4a2713aSLionel Sambuc if (R->isInjectedClassName())
555f4a2713aSLionel Sambuc continue;
556f4a2713aSLionel Sambuc if (R->hasDefinition())
557f4a2713aSLionel Sambuc Complete = MethodsAndNestedClassesComplete(R->getDefinition(),
558f4a2713aSLionel Sambuc MNCComplete);
559f4a2713aSLionel Sambuc else
560f4a2713aSLionel Sambuc Complete = false;
561f4a2713aSLionel Sambuc }
562f4a2713aSLionel Sambuc }
563f4a2713aSLionel Sambuc MNCComplete[RD] = Complete;
564f4a2713aSLionel Sambuc return Complete;
565f4a2713aSLionel Sambuc }
566f4a2713aSLionel Sambuc
567f4a2713aSLionel Sambuc /// \brief Returns true, if the given CXXRecordDecl is fully defined in this
568f4a2713aSLionel Sambuc /// translation unit, i.e. all methods are defined or pure virtual and all
569f4a2713aSLionel Sambuc /// friends, friend functions and nested classes are fully defined in this
570f4a2713aSLionel Sambuc /// translation unit.
571f4a2713aSLionel Sambuc ///
572f4a2713aSLionel Sambuc /// Should only be called from ActOnEndOfTranslationUnit so that all
573f4a2713aSLionel Sambuc /// definitions are actually read.
IsRecordFullyDefined(const CXXRecordDecl * RD,RecordCompleteMap & RecordsComplete,RecordCompleteMap & MNCComplete)574f4a2713aSLionel Sambuc static bool IsRecordFullyDefined(const CXXRecordDecl *RD,
575f4a2713aSLionel Sambuc RecordCompleteMap &RecordsComplete,
576f4a2713aSLionel Sambuc RecordCompleteMap &MNCComplete) {
577f4a2713aSLionel Sambuc RecordCompleteMap::iterator Cache = RecordsComplete.find(RD);
578f4a2713aSLionel Sambuc if (Cache != RecordsComplete.end())
579f4a2713aSLionel Sambuc return Cache->second;
580f4a2713aSLionel Sambuc bool Complete = MethodsAndNestedClassesComplete(RD, MNCComplete);
581f4a2713aSLionel Sambuc for (CXXRecordDecl::friend_iterator I = RD->friend_begin(),
582f4a2713aSLionel Sambuc E = RD->friend_end();
583f4a2713aSLionel Sambuc I != E && Complete; ++I) {
584f4a2713aSLionel Sambuc // Check if friend classes and methods are complete.
585f4a2713aSLionel Sambuc if (TypeSourceInfo *TSI = (*I)->getFriendType()) {
586f4a2713aSLionel Sambuc // Friend classes are available as the TypeSourceInfo of the FriendDecl.
587f4a2713aSLionel Sambuc if (CXXRecordDecl *FriendD = TSI->getType()->getAsCXXRecordDecl())
588f4a2713aSLionel Sambuc Complete = MethodsAndNestedClassesComplete(FriendD, MNCComplete);
589f4a2713aSLionel Sambuc else
590f4a2713aSLionel Sambuc Complete = false;
591f4a2713aSLionel Sambuc } else {
592f4a2713aSLionel Sambuc // Friend functions are available through the NamedDecl of FriendDecl.
593f4a2713aSLionel Sambuc if (const FunctionDecl *FD =
594f4a2713aSLionel Sambuc dyn_cast<FunctionDecl>((*I)->getFriendDecl()))
595f4a2713aSLionel Sambuc Complete = FD->isDefined();
596f4a2713aSLionel Sambuc else
597f4a2713aSLionel Sambuc // This is a template friend, give up.
598f4a2713aSLionel Sambuc Complete = false;
599f4a2713aSLionel Sambuc }
600f4a2713aSLionel Sambuc }
601f4a2713aSLionel Sambuc RecordsComplete[RD] = Complete;
602f4a2713aSLionel Sambuc return Complete;
603f4a2713aSLionel Sambuc }
604f4a2713aSLionel Sambuc
emitAndClearUnusedLocalTypedefWarnings()605*0a6a1f1dSLionel Sambuc void Sema::emitAndClearUnusedLocalTypedefWarnings() {
606*0a6a1f1dSLionel Sambuc if (ExternalSource)
607*0a6a1f1dSLionel Sambuc ExternalSource->ReadUnusedLocalTypedefNameCandidates(
608*0a6a1f1dSLionel Sambuc UnusedLocalTypedefNameCandidates);
609*0a6a1f1dSLionel Sambuc for (const TypedefNameDecl *TD : UnusedLocalTypedefNameCandidates) {
610*0a6a1f1dSLionel Sambuc if (TD->isReferenced())
611*0a6a1f1dSLionel Sambuc continue;
612*0a6a1f1dSLionel Sambuc Diag(TD->getLocation(), diag::warn_unused_local_typedef)
613*0a6a1f1dSLionel Sambuc << isa<TypeAliasDecl>(TD) << TD->getDeclName();
614*0a6a1f1dSLionel Sambuc }
615*0a6a1f1dSLionel Sambuc UnusedLocalTypedefNameCandidates.clear();
616*0a6a1f1dSLionel Sambuc }
617*0a6a1f1dSLionel Sambuc
618f4a2713aSLionel Sambuc /// ActOnEndOfTranslationUnit - This is called at the very end of the
619f4a2713aSLionel Sambuc /// translation unit when EOF is reached and all but the top-level scope is
620f4a2713aSLionel Sambuc /// popped.
ActOnEndOfTranslationUnit()621f4a2713aSLionel Sambuc void Sema::ActOnEndOfTranslationUnit() {
622*0a6a1f1dSLionel Sambuc assert(DelayedDiagnostics.getCurrentPool() == nullptr
623f4a2713aSLionel Sambuc && "reached end of translation unit with a pool attached?");
624f4a2713aSLionel Sambuc
625f4a2713aSLionel Sambuc // If code completion is enabled, don't perform any end-of-translation-unit
626f4a2713aSLionel Sambuc // work.
627f4a2713aSLionel Sambuc if (PP.isCodeCompletionEnabled())
628f4a2713aSLionel Sambuc return;
629f4a2713aSLionel Sambuc
630f4a2713aSLionel Sambuc // Complete translation units and modules define vtables and perform implicit
631f4a2713aSLionel Sambuc // instantiations. PCH files do not.
632f4a2713aSLionel Sambuc if (TUKind != TU_Prefix) {
633f4a2713aSLionel Sambuc DiagnoseUseOfUnimplementedSelectors();
634f4a2713aSLionel Sambuc
635f4a2713aSLionel Sambuc // If any dynamic classes have their key function defined within
636f4a2713aSLionel Sambuc // this translation unit, then those vtables are considered "used" and must
637f4a2713aSLionel Sambuc // be emitted.
638f4a2713aSLionel Sambuc for (DynamicClassesType::iterator I = DynamicClasses.begin(ExternalSource),
639f4a2713aSLionel Sambuc E = DynamicClasses.end();
640f4a2713aSLionel Sambuc I != E; ++I) {
641f4a2713aSLionel Sambuc assert(!(*I)->isDependentType() &&
642f4a2713aSLionel Sambuc "Should not see dependent types here!");
643*0a6a1f1dSLionel Sambuc if (const CXXMethodDecl *KeyFunction =
644*0a6a1f1dSLionel Sambuc Context.getCurrentKeyFunction(*I)) {
645*0a6a1f1dSLionel Sambuc const FunctionDecl *Definition = nullptr;
646f4a2713aSLionel Sambuc if (KeyFunction->hasBody(Definition))
647f4a2713aSLionel Sambuc MarkVTableUsed(Definition->getLocation(), *I, true);
648f4a2713aSLionel Sambuc }
649f4a2713aSLionel Sambuc }
650f4a2713aSLionel Sambuc
651f4a2713aSLionel Sambuc // If DefinedUsedVTables ends up marking any virtual member functions it
652f4a2713aSLionel Sambuc // might lead to more pending template instantiations, which we then need
653f4a2713aSLionel Sambuc // to instantiate.
654f4a2713aSLionel Sambuc DefineUsedVTables();
655f4a2713aSLionel Sambuc
656f4a2713aSLionel Sambuc // C++: Perform implicit template instantiations.
657f4a2713aSLionel Sambuc //
658f4a2713aSLionel Sambuc // FIXME: When we perform these implicit instantiations, we do not
659f4a2713aSLionel Sambuc // carefully keep track of the point of instantiation (C++ [temp.point]).
660f4a2713aSLionel Sambuc // This means that name lookup that occurs within the template
661f4a2713aSLionel Sambuc // instantiation will always happen at the end of the translation unit,
662f4a2713aSLionel Sambuc // so it will find some names that are not required to be found. This is
663f4a2713aSLionel Sambuc // valid, but we could do better by diagnosing if an instantiation uses a
664f4a2713aSLionel Sambuc // name that was not visible at its first point of instantiation.
665*0a6a1f1dSLionel Sambuc if (ExternalSource) {
666*0a6a1f1dSLionel Sambuc // Load pending instantiations from the external source.
667*0a6a1f1dSLionel Sambuc SmallVector<PendingImplicitInstantiation, 4> Pending;
668*0a6a1f1dSLionel Sambuc ExternalSource->ReadPendingInstantiations(Pending);
669*0a6a1f1dSLionel Sambuc PendingInstantiations.insert(PendingInstantiations.begin(),
670*0a6a1f1dSLionel Sambuc Pending.begin(), Pending.end());
671*0a6a1f1dSLionel Sambuc }
672f4a2713aSLionel Sambuc PerformPendingInstantiations();
673*0a6a1f1dSLionel Sambuc
674*0a6a1f1dSLionel Sambuc if (LateTemplateParserCleanup)
675*0a6a1f1dSLionel Sambuc LateTemplateParserCleanup(OpaqueParser);
676*0a6a1f1dSLionel Sambuc
677f4a2713aSLionel Sambuc CheckDelayedMemberExceptionSpecs();
678f4a2713aSLionel Sambuc }
679f4a2713aSLionel Sambuc
680f4a2713aSLionel Sambuc // All delayed member exception specs should be checked or we end up accepting
681f4a2713aSLionel Sambuc // incompatible declarations.
682f4a2713aSLionel Sambuc assert(DelayedDefaultedMemberExceptionSpecs.empty());
683*0a6a1f1dSLionel Sambuc assert(DelayedExceptionSpecChecks.empty());
684f4a2713aSLionel Sambuc
685f4a2713aSLionel Sambuc // Remove file scoped decls that turned out to be used.
686f4a2713aSLionel Sambuc UnusedFileScopedDecls.erase(
687*0a6a1f1dSLionel Sambuc std::remove_if(UnusedFileScopedDecls.begin(nullptr, true),
688f4a2713aSLionel Sambuc UnusedFileScopedDecls.end(),
689f4a2713aSLionel Sambuc std::bind1st(std::ptr_fun(ShouldRemoveFromUnused), this)),
690f4a2713aSLionel Sambuc UnusedFileScopedDecls.end());
691f4a2713aSLionel Sambuc
692f4a2713aSLionel Sambuc if (TUKind == TU_Prefix) {
693f4a2713aSLionel Sambuc // Translation unit prefixes don't need any of the checking below.
694*0a6a1f1dSLionel Sambuc TUScope = nullptr;
695f4a2713aSLionel Sambuc return;
696f4a2713aSLionel Sambuc }
697f4a2713aSLionel Sambuc
698f4a2713aSLionel Sambuc // Check for #pragma weak identifiers that were never declared
699f4a2713aSLionel Sambuc // FIXME: This will cause diagnostics to be emitted in a non-determinstic
700f4a2713aSLionel Sambuc // order! Iterating over a densemap like this is bad.
701f4a2713aSLionel Sambuc LoadExternalWeakUndeclaredIdentifiers();
702f4a2713aSLionel Sambuc for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
703f4a2713aSLionel Sambuc I = WeakUndeclaredIdentifiers.begin(),
704f4a2713aSLionel Sambuc E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
705f4a2713aSLionel Sambuc if (I->second.getUsed()) continue;
706f4a2713aSLionel Sambuc
707f4a2713aSLionel Sambuc Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
708f4a2713aSLionel Sambuc << I->first;
709f4a2713aSLionel Sambuc }
710f4a2713aSLionel Sambuc
711f4a2713aSLionel Sambuc if (LangOpts.CPlusPlus11 &&
712*0a6a1f1dSLionel Sambuc !Diags.isIgnored(diag::warn_delegating_ctor_cycle, SourceLocation()))
713f4a2713aSLionel Sambuc CheckDelegatingCtorCycles();
714f4a2713aSLionel Sambuc
715f4a2713aSLionel Sambuc if (TUKind == TU_Module) {
716f4a2713aSLionel Sambuc // If we are building a module, resolve all of the exported declarations
717f4a2713aSLionel Sambuc // now.
718f4a2713aSLionel Sambuc if (Module *CurrentModule = PP.getCurrentModule()) {
719f4a2713aSLionel Sambuc ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
720f4a2713aSLionel Sambuc
721f4a2713aSLionel Sambuc SmallVector<Module *, 2> Stack;
722f4a2713aSLionel Sambuc Stack.push_back(CurrentModule);
723f4a2713aSLionel Sambuc while (!Stack.empty()) {
724f4a2713aSLionel Sambuc Module *Mod = Stack.pop_back_val();
725f4a2713aSLionel Sambuc
726f4a2713aSLionel Sambuc // Resolve the exported declarations and conflicts.
727f4a2713aSLionel Sambuc // FIXME: Actually complain, once we figure out how to teach the
728f4a2713aSLionel Sambuc // diagnostic client to deal with complaints in the module map at this
729f4a2713aSLionel Sambuc // point.
730f4a2713aSLionel Sambuc ModMap.resolveExports(Mod, /*Complain=*/false);
731f4a2713aSLionel Sambuc ModMap.resolveUses(Mod, /*Complain=*/false);
732f4a2713aSLionel Sambuc ModMap.resolveConflicts(Mod, /*Complain=*/false);
733f4a2713aSLionel Sambuc
734f4a2713aSLionel Sambuc // Queue the submodules, so their exports will also be resolved.
735f4a2713aSLionel Sambuc for (Module::submodule_iterator Sub = Mod->submodule_begin(),
736f4a2713aSLionel Sambuc SubEnd = Mod->submodule_end();
737f4a2713aSLionel Sambuc Sub != SubEnd; ++Sub) {
738f4a2713aSLionel Sambuc Stack.push_back(*Sub);
739f4a2713aSLionel Sambuc }
740f4a2713aSLionel Sambuc }
741f4a2713aSLionel Sambuc }
742f4a2713aSLionel Sambuc
743*0a6a1f1dSLionel Sambuc // Warnings emitted in ActOnEndOfTranslationUnit() should be emitted for
744*0a6a1f1dSLionel Sambuc // modules when they are built, not every time they are used.
745*0a6a1f1dSLionel Sambuc emitAndClearUnusedLocalTypedefWarnings();
746*0a6a1f1dSLionel Sambuc
747f4a2713aSLionel Sambuc // Modules don't need any of the checking below.
748*0a6a1f1dSLionel Sambuc TUScope = nullptr;
749f4a2713aSLionel Sambuc return;
750f4a2713aSLionel Sambuc }
751f4a2713aSLionel Sambuc
752f4a2713aSLionel Sambuc // C99 6.9.2p2:
753f4a2713aSLionel Sambuc // A declaration of an identifier for an object that has file
754f4a2713aSLionel Sambuc // scope without an initializer, and without a storage-class
755f4a2713aSLionel Sambuc // specifier or with the storage-class specifier static,
756f4a2713aSLionel Sambuc // constitutes a tentative definition. If a translation unit
757f4a2713aSLionel Sambuc // contains one or more tentative definitions for an identifier,
758f4a2713aSLionel Sambuc // and the translation unit contains no external definition for
759f4a2713aSLionel Sambuc // that identifier, then the behavior is exactly as if the
760f4a2713aSLionel Sambuc // translation unit contains a file scope declaration of that
761f4a2713aSLionel Sambuc // identifier, with the composite type as of the end of the
762f4a2713aSLionel Sambuc // translation unit, with an initializer equal to 0.
763f4a2713aSLionel Sambuc llvm::SmallSet<VarDecl *, 32> Seen;
764f4a2713aSLionel Sambuc for (TentativeDefinitionsType::iterator
765f4a2713aSLionel Sambuc T = TentativeDefinitions.begin(ExternalSource),
766f4a2713aSLionel Sambuc TEnd = TentativeDefinitions.end();
767f4a2713aSLionel Sambuc T != TEnd; ++T)
768f4a2713aSLionel Sambuc {
769f4a2713aSLionel Sambuc VarDecl *VD = (*T)->getActingDefinition();
770f4a2713aSLionel Sambuc
771f4a2713aSLionel Sambuc // If the tentative definition was completed, getActingDefinition() returns
772f4a2713aSLionel Sambuc // null. If we've already seen this variable before, insert()'s second
773f4a2713aSLionel Sambuc // return value is false.
774*0a6a1f1dSLionel Sambuc if (!VD || VD->isInvalidDecl() || !Seen.insert(VD).second)
775f4a2713aSLionel Sambuc continue;
776f4a2713aSLionel Sambuc
777f4a2713aSLionel Sambuc if (const IncompleteArrayType *ArrayT
778f4a2713aSLionel Sambuc = Context.getAsIncompleteArrayType(VD->getType())) {
779f4a2713aSLionel Sambuc // Set the length of the array to 1 (C99 6.9.2p5).
780f4a2713aSLionel Sambuc Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
781f4a2713aSLionel Sambuc llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
782f4a2713aSLionel Sambuc QualType T = Context.getConstantArrayType(ArrayT->getElementType(),
783f4a2713aSLionel Sambuc One, ArrayType::Normal, 0);
784f4a2713aSLionel Sambuc VD->setType(T);
785f4a2713aSLionel Sambuc } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
786f4a2713aSLionel Sambuc diag::err_tentative_def_incomplete_type))
787f4a2713aSLionel Sambuc VD->setInvalidDecl();
788f4a2713aSLionel Sambuc
789f4a2713aSLionel Sambuc CheckCompleteVariableDeclaration(VD);
790f4a2713aSLionel Sambuc
791f4a2713aSLionel Sambuc // Notify the consumer that we've completed a tentative definition.
792f4a2713aSLionel Sambuc if (!VD->isInvalidDecl())
793f4a2713aSLionel Sambuc Consumer.CompleteTentativeDefinition(VD);
794f4a2713aSLionel Sambuc
795f4a2713aSLionel Sambuc }
796f4a2713aSLionel Sambuc
797f4a2713aSLionel Sambuc // If there were errors, disable 'unused' warnings since they will mostly be
798f4a2713aSLionel Sambuc // noise.
799f4a2713aSLionel Sambuc if (!Diags.hasErrorOccurred()) {
800f4a2713aSLionel Sambuc // Output warning for unused file scoped decls.
801f4a2713aSLionel Sambuc for (UnusedFileScopedDeclsType::iterator
802f4a2713aSLionel Sambuc I = UnusedFileScopedDecls.begin(ExternalSource),
803f4a2713aSLionel Sambuc E = UnusedFileScopedDecls.end(); I != E; ++I) {
804f4a2713aSLionel Sambuc if (ShouldRemoveFromUnused(this, *I))
805f4a2713aSLionel Sambuc continue;
806f4a2713aSLionel Sambuc
807f4a2713aSLionel Sambuc if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
808f4a2713aSLionel Sambuc const FunctionDecl *DiagD;
809f4a2713aSLionel Sambuc if (!FD->hasBody(DiagD))
810f4a2713aSLionel Sambuc DiagD = FD;
811f4a2713aSLionel Sambuc if (DiagD->isDeleted())
812f4a2713aSLionel Sambuc continue; // Deleted functions are supposed to be unused.
813f4a2713aSLionel Sambuc if (DiagD->isReferenced()) {
814f4a2713aSLionel Sambuc if (isa<CXXMethodDecl>(DiagD))
815f4a2713aSLionel Sambuc Diag(DiagD->getLocation(), diag::warn_unneeded_member_function)
816f4a2713aSLionel Sambuc << DiagD->getDeclName();
817f4a2713aSLionel Sambuc else {
818f4a2713aSLionel Sambuc if (FD->getStorageClass() == SC_Static &&
819f4a2713aSLionel Sambuc !FD->isInlineSpecified() &&
820f4a2713aSLionel Sambuc !SourceMgr.isInMainFile(
821f4a2713aSLionel Sambuc SourceMgr.getExpansionLoc(FD->getLocation())))
822*0a6a1f1dSLionel Sambuc Diag(DiagD->getLocation(),
823*0a6a1f1dSLionel Sambuc diag::warn_unneeded_static_internal_decl)
824f4a2713aSLionel Sambuc << DiagD->getDeclName();
825f4a2713aSLionel Sambuc else
826f4a2713aSLionel Sambuc Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl)
827f4a2713aSLionel Sambuc << /*function*/0 << DiagD->getDeclName();
828f4a2713aSLionel Sambuc }
829f4a2713aSLionel Sambuc } else {
830f4a2713aSLionel Sambuc Diag(DiagD->getLocation(),
831f4a2713aSLionel Sambuc isa<CXXMethodDecl>(DiagD) ? diag::warn_unused_member_function
832f4a2713aSLionel Sambuc : diag::warn_unused_function)
833f4a2713aSLionel Sambuc << DiagD->getDeclName();
834f4a2713aSLionel Sambuc }
835f4a2713aSLionel Sambuc } else {
836f4a2713aSLionel Sambuc const VarDecl *DiagD = cast<VarDecl>(*I)->getDefinition();
837f4a2713aSLionel Sambuc if (!DiagD)
838f4a2713aSLionel Sambuc DiagD = cast<VarDecl>(*I);
839f4a2713aSLionel Sambuc if (DiagD->isReferenced()) {
840f4a2713aSLionel Sambuc Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl)
841f4a2713aSLionel Sambuc << /*variable*/1 << DiagD->getDeclName();
842f4a2713aSLionel Sambuc } else if (DiagD->getType().isConstQualified()) {
843f4a2713aSLionel Sambuc Diag(DiagD->getLocation(), diag::warn_unused_const_variable)
844f4a2713aSLionel Sambuc << DiagD->getDeclName();
845f4a2713aSLionel Sambuc } else {
846f4a2713aSLionel Sambuc Diag(DiagD->getLocation(), diag::warn_unused_variable)
847f4a2713aSLionel Sambuc << DiagD->getDeclName();
848f4a2713aSLionel Sambuc }
849f4a2713aSLionel Sambuc }
850f4a2713aSLionel Sambuc }
851f4a2713aSLionel Sambuc
852f4a2713aSLionel Sambuc if (ExternalSource)
853f4a2713aSLionel Sambuc ExternalSource->ReadUndefinedButUsed(UndefinedButUsed);
854f4a2713aSLionel Sambuc checkUndefinedButUsed(*this);
855*0a6a1f1dSLionel Sambuc
856*0a6a1f1dSLionel Sambuc emitAndClearUnusedLocalTypedefWarnings();
857f4a2713aSLionel Sambuc }
858f4a2713aSLionel Sambuc
859*0a6a1f1dSLionel Sambuc if (!Diags.isIgnored(diag::warn_unused_private_field, SourceLocation())) {
860f4a2713aSLionel Sambuc RecordCompleteMap RecordsComplete;
861f4a2713aSLionel Sambuc RecordCompleteMap MNCComplete;
862f4a2713aSLionel Sambuc for (NamedDeclSetType::iterator I = UnusedPrivateFields.begin(),
863f4a2713aSLionel Sambuc E = UnusedPrivateFields.end(); I != E; ++I) {
864f4a2713aSLionel Sambuc const NamedDecl *D = *I;
865f4a2713aSLionel Sambuc const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext());
866f4a2713aSLionel Sambuc if (RD && !RD->isUnion() &&
867f4a2713aSLionel Sambuc IsRecordFullyDefined(RD, RecordsComplete, MNCComplete)) {
868f4a2713aSLionel Sambuc Diag(D->getLocation(), diag::warn_unused_private_field)
869f4a2713aSLionel Sambuc << D->getDeclName();
870f4a2713aSLionel Sambuc }
871f4a2713aSLionel Sambuc }
872f4a2713aSLionel Sambuc }
873f4a2713aSLionel Sambuc
874f4a2713aSLionel Sambuc // Check we've noticed that we're no longer parsing the initializer for every
875f4a2713aSLionel Sambuc // variable. If we miss cases, then at best we have a performance issue and
876f4a2713aSLionel Sambuc // at worst a rejects-valid bug.
877f4a2713aSLionel Sambuc assert(ParsingInitForAutoVars.empty() &&
878f4a2713aSLionel Sambuc "Didn't unmark var as having its initializer parsed");
879f4a2713aSLionel Sambuc
880*0a6a1f1dSLionel Sambuc TUScope = nullptr;
881f4a2713aSLionel Sambuc }
882f4a2713aSLionel Sambuc
883f4a2713aSLionel Sambuc
884f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
885f4a2713aSLionel Sambuc // Helper functions.
886f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
887f4a2713aSLionel Sambuc
getFunctionLevelDeclContext()888f4a2713aSLionel Sambuc DeclContext *Sema::getFunctionLevelDeclContext() {
889f4a2713aSLionel Sambuc DeclContext *DC = CurContext;
890f4a2713aSLionel Sambuc
891f4a2713aSLionel Sambuc while (true) {
892f4a2713aSLionel Sambuc if (isa<BlockDecl>(DC) || isa<EnumDecl>(DC) || isa<CapturedDecl>(DC)) {
893f4a2713aSLionel Sambuc DC = DC->getParent();
894f4a2713aSLionel Sambuc } else if (isa<CXXMethodDecl>(DC) &&
895f4a2713aSLionel Sambuc cast<CXXMethodDecl>(DC)->getOverloadedOperator() == OO_Call &&
896f4a2713aSLionel Sambuc cast<CXXRecordDecl>(DC->getParent())->isLambda()) {
897f4a2713aSLionel Sambuc DC = DC->getParent()->getParent();
898f4a2713aSLionel Sambuc }
899f4a2713aSLionel Sambuc else break;
900f4a2713aSLionel Sambuc }
901f4a2713aSLionel Sambuc
902f4a2713aSLionel Sambuc return DC;
903f4a2713aSLionel Sambuc }
904f4a2713aSLionel Sambuc
905f4a2713aSLionel Sambuc /// getCurFunctionDecl - If inside of a function body, this returns a pointer
906f4a2713aSLionel Sambuc /// to the function decl for the function being parsed. If we're currently
907f4a2713aSLionel Sambuc /// in a 'block', this returns the containing context.
getCurFunctionDecl()908f4a2713aSLionel Sambuc FunctionDecl *Sema::getCurFunctionDecl() {
909f4a2713aSLionel Sambuc DeclContext *DC = getFunctionLevelDeclContext();
910f4a2713aSLionel Sambuc return dyn_cast<FunctionDecl>(DC);
911f4a2713aSLionel Sambuc }
912f4a2713aSLionel Sambuc
getCurMethodDecl()913f4a2713aSLionel Sambuc ObjCMethodDecl *Sema::getCurMethodDecl() {
914f4a2713aSLionel Sambuc DeclContext *DC = getFunctionLevelDeclContext();
915f4a2713aSLionel Sambuc while (isa<RecordDecl>(DC))
916f4a2713aSLionel Sambuc DC = DC->getParent();
917f4a2713aSLionel Sambuc return dyn_cast<ObjCMethodDecl>(DC);
918f4a2713aSLionel Sambuc }
919f4a2713aSLionel Sambuc
getCurFunctionOrMethodDecl()920f4a2713aSLionel Sambuc NamedDecl *Sema::getCurFunctionOrMethodDecl() {
921f4a2713aSLionel Sambuc DeclContext *DC = getFunctionLevelDeclContext();
922f4a2713aSLionel Sambuc if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
923f4a2713aSLionel Sambuc return cast<NamedDecl>(DC);
924*0a6a1f1dSLionel Sambuc return nullptr;
925f4a2713aSLionel Sambuc }
926f4a2713aSLionel Sambuc
EmitCurrentDiagnostic(unsigned DiagID)927f4a2713aSLionel Sambuc void Sema::EmitCurrentDiagnostic(unsigned DiagID) {
928f4a2713aSLionel Sambuc // FIXME: It doesn't make sense to me that DiagID is an incoming argument here
929f4a2713aSLionel Sambuc // and yet we also use the current diag ID on the DiagnosticsEngine. This has
930f4a2713aSLionel Sambuc // been made more painfully obvious by the refactor that introduced this
931f4a2713aSLionel Sambuc // function, but it is possible that the incoming argument can be
932f4a2713aSLionel Sambuc // eliminnated. If it truly cannot be (for example, there is some reentrancy
933f4a2713aSLionel Sambuc // issue I am not seeing yet), then there should at least be a clarifying
934f4a2713aSLionel Sambuc // comment somewhere.
935f4a2713aSLionel Sambuc if (Optional<TemplateDeductionInfo*> Info = isSFINAEContext()) {
936f4a2713aSLionel Sambuc switch (DiagnosticIDs::getDiagnosticSFINAEResponse(
937f4a2713aSLionel Sambuc Diags.getCurrentDiagID())) {
938f4a2713aSLionel Sambuc case DiagnosticIDs::SFINAE_Report:
939f4a2713aSLionel Sambuc // We'll report the diagnostic below.
940f4a2713aSLionel Sambuc break;
941f4a2713aSLionel Sambuc
942f4a2713aSLionel Sambuc case DiagnosticIDs::SFINAE_SubstitutionFailure:
943f4a2713aSLionel Sambuc // Count this failure so that we know that template argument deduction
944f4a2713aSLionel Sambuc // has failed.
945f4a2713aSLionel Sambuc ++NumSFINAEErrors;
946f4a2713aSLionel Sambuc
947f4a2713aSLionel Sambuc // Make a copy of this suppressed diagnostic and store it with the
948f4a2713aSLionel Sambuc // template-deduction information.
949f4a2713aSLionel Sambuc if (*Info && !(*Info)->hasSFINAEDiagnostic()) {
950f4a2713aSLionel Sambuc Diagnostic DiagInfo(&Diags);
951f4a2713aSLionel Sambuc (*Info)->addSFINAEDiagnostic(DiagInfo.getLocation(),
952f4a2713aSLionel Sambuc PartialDiagnostic(DiagInfo, Context.getDiagAllocator()));
953f4a2713aSLionel Sambuc }
954f4a2713aSLionel Sambuc
955f4a2713aSLionel Sambuc Diags.setLastDiagnosticIgnored();
956f4a2713aSLionel Sambuc Diags.Clear();
957f4a2713aSLionel Sambuc return;
958f4a2713aSLionel Sambuc
959f4a2713aSLionel Sambuc case DiagnosticIDs::SFINAE_AccessControl: {
960f4a2713aSLionel Sambuc // Per C++ Core Issue 1170, access control is part of SFINAE.
961f4a2713aSLionel Sambuc // Additionally, the AccessCheckingSFINAE flag can be used to temporarily
962f4a2713aSLionel Sambuc // make access control a part of SFINAE for the purposes of checking
963f4a2713aSLionel Sambuc // type traits.
964f4a2713aSLionel Sambuc if (!AccessCheckingSFINAE && !getLangOpts().CPlusPlus11)
965f4a2713aSLionel Sambuc break;
966f4a2713aSLionel Sambuc
967f4a2713aSLionel Sambuc SourceLocation Loc = Diags.getCurrentDiagLoc();
968f4a2713aSLionel Sambuc
969f4a2713aSLionel Sambuc // Suppress this diagnostic.
970f4a2713aSLionel Sambuc ++NumSFINAEErrors;
971f4a2713aSLionel Sambuc
972f4a2713aSLionel Sambuc // Make a copy of this suppressed diagnostic and store it with the
973f4a2713aSLionel Sambuc // template-deduction information.
974f4a2713aSLionel Sambuc if (*Info && !(*Info)->hasSFINAEDiagnostic()) {
975f4a2713aSLionel Sambuc Diagnostic DiagInfo(&Diags);
976f4a2713aSLionel Sambuc (*Info)->addSFINAEDiagnostic(DiagInfo.getLocation(),
977f4a2713aSLionel Sambuc PartialDiagnostic(DiagInfo, Context.getDiagAllocator()));
978f4a2713aSLionel Sambuc }
979f4a2713aSLionel Sambuc
980f4a2713aSLionel Sambuc Diags.setLastDiagnosticIgnored();
981f4a2713aSLionel Sambuc Diags.Clear();
982f4a2713aSLionel Sambuc
983f4a2713aSLionel Sambuc // Now the diagnostic state is clear, produce a C++98 compatibility
984f4a2713aSLionel Sambuc // warning.
985f4a2713aSLionel Sambuc Diag(Loc, diag::warn_cxx98_compat_sfinae_access_control);
986f4a2713aSLionel Sambuc
987f4a2713aSLionel Sambuc // The last diagnostic which Sema produced was ignored. Suppress any
988f4a2713aSLionel Sambuc // notes attached to it.
989f4a2713aSLionel Sambuc Diags.setLastDiagnosticIgnored();
990f4a2713aSLionel Sambuc return;
991f4a2713aSLionel Sambuc }
992f4a2713aSLionel Sambuc
993f4a2713aSLionel Sambuc case DiagnosticIDs::SFINAE_Suppress:
994f4a2713aSLionel Sambuc // Make a copy of this suppressed diagnostic and store it with the
995f4a2713aSLionel Sambuc // template-deduction information;
996f4a2713aSLionel Sambuc if (*Info) {
997f4a2713aSLionel Sambuc Diagnostic DiagInfo(&Diags);
998f4a2713aSLionel Sambuc (*Info)->addSuppressedDiagnostic(DiagInfo.getLocation(),
999f4a2713aSLionel Sambuc PartialDiagnostic(DiagInfo, Context.getDiagAllocator()));
1000f4a2713aSLionel Sambuc }
1001f4a2713aSLionel Sambuc
1002f4a2713aSLionel Sambuc // Suppress this diagnostic.
1003f4a2713aSLionel Sambuc Diags.setLastDiagnosticIgnored();
1004f4a2713aSLionel Sambuc Diags.Clear();
1005f4a2713aSLionel Sambuc return;
1006f4a2713aSLionel Sambuc }
1007f4a2713aSLionel Sambuc }
1008f4a2713aSLionel Sambuc
1009f4a2713aSLionel Sambuc // Set up the context's printing policy based on our current state.
1010f4a2713aSLionel Sambuc Context.setPrintingPolicy(getPrintingPolicy());
1011f4a2713aSLionel Sambuc
1012f4a2713aSLionel Sambuc // Emit the diagnostic.
1013f4a2713aSLionel Sambuc if (!Diags.EmitCurrentDiagnostic())
1014f4a2713aSLionel Sambuc return;
1015f4a2713aSLionel Sambuc
1016f4a2713aSLionel Sambuc // If this is not a note, and we're in a template instantiation
1017f4a2713aSLionel Sambuc // that is different from the last template instantiation where
1018f4a2713aSLionel Sambuc // we emitted an error, print a template instantiation
1019f4a2713aSLionel Sambuc // backtrace.
1020f4a2713aSLionel Sambuc if (!DiagnosticIDs::isBuiltinNote(DiagID) &&
1021f4a2713aSLionel Sambuc !ActiveTemplateInstantiations.empty() &&
1022f4a2713aSLionel Sambuc ActiveTemplateInstantiations.back()
1023f4a2713aSLionel Sambuc != LastTemplateInstantiationErrorContext) {
1024f4a2713aSLionel Sambuc PrintInstantiationStack();
1025f4a2713aSLionel Sambuc LastTemplateInstantiationErrorContext = ActiveTemplateInstantiations.back();
1026f4a2713aSLionel Sambuc }
1027f4a2713aSLionel Sambuc }
1028f4a2713aSLionel Sambuc
1029f4a2713aSLionel Sambuc Sema::SemaDiagnosticBuilder
Diag(SourceLocation Loc,const PartialDiagnostic & PD)1030f4a2713aSLionel Sambuc Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) {
1031f4a2713aSLionel Sambuc SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID()));
1032f4a2713aSLionel Sambuc PD.Emit(Builder);
1033f4a2713aSLionel Sambuc
1034f4a2713aSLionel Sambuc return Builder;
1035f4a2713aSLionel Sambuc }
1036f4a2713aSLionel Sambuc
1037f4a2713aSLionel Sambuc /// \brief Looks through the macro-expansion chain for the given
1038f4a2713aSLionel Sambuc /// location, looking for a macro expansion with the given name.
1039f4a2713aSLionel Sambuc /// If one is found, returns true and sets the location to that
1040f4a2713aSLionel Sambuc /// expansion loc.
findMacroSpelling(SourceLocation & locref,StringRef name)1041f4a2713aSLionel Sambuc bool Sema::findMacroSpelling(SourceLocation &locref, StringRef name) {
1042f4a2713aSLionel Sambuc SourceLocation loc = locref;
1043f4a2713aSLionel Sambuc if (!loc.isMacroID()) return false;
1044f4a2713aSLionel Sambuc
1045f4a2713aSLionel Sambuc // There's no good way right now to look at the intermediate
1046f4a2713aSLionel Sambuc // expansions, so just jump to the expansion location.
1047f4a2713aSLionel Sambuc loc = getSourceManager().getExpansionLoc(loc);
1048f4a2713aSLionel Sambuc
1049f4a2713aSLionel Sambuc // If that's written with the name, stop here.
1050f4a2713aSLionel Sambuc SmallVector<char, 16> buffer;
1051f4a2713aSLionel Sambuc if (getPreprocessor().getSpelling(loc, buffer) == name) {
1052f4a2713aSLionel Sambuc locref = loc;
1053f4a2713aSLionel Sambuc return true;
1054f4a2713aSLionel Sambuc }
1055f4a2713aSLionel Sambuc return false;
1056f4a2713aSLionel Sambuc }
1057f4a2713aSLionel Sambuc
1058f4a2713aSLionel Sambuc /// \brief Determines the active Scope associated with the given declaration
1059f4a2713aSLionel Sambuc /// context.
1060f4a2713aSLionel Sambuc ///
1061f4a2713aSLionel Sambuc /// This routine maps a declaration context to the active Scope object that
1062f4a2713aSLionel Sambuc /// represents that declaration context in the parser. It is typically used
1063f4a2713aSLionel Sambuc /// from "scope-less" code (e.g., template instantiation, lazy creation of
1064f4a2713aSLionel Sambuc /// declarations) that injects a name for name-lookup purposes and, therefore,
1065f4a2713aSLionel Sambuc /// must update the Scope.
1066f4a2713aSLionel Sambuc ///
1067f4a2713aSLionel Sambuc /// \returns The scope corresponding to the given declaraion context, or NULL
1068f4a2713aSLionel Sambuc /// if no such scope is open.
getScopeForContext(DeclContext * Ctx)1069f4a2713aSLionel Sambuc Scope *Sema::getScopeForContext(DeclContext *Ctx) {
1070f4a2713aSLionel Sambuc
1071f4a2713aSLionel Sambuc if (!Ctx)
1072*0a6a1f1dSLionel Sambuc return nullptr;
1073f4a2713aSLionel Sambuc
1074f4a2713aSLionel Sambuc Ctx = Ctx->getPrimaryContext();
1075f4a2713aSLionel Sambuc for (Scope *S = getCurScope(); S; S = S->getParent()) {
1076f4a2713aSLionel Sambuc // Ignore scopes that cannot have declarations. This is important for
1077f4a2713aSLionel Sambuc // out-of-line definitions of static class members.
1078f4a2713aSLionel Sambuc if (S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope))
1079f4a2713aSLionel Sambuc if (DeclContext *Entity = S->getEntity())
1080f4a2713aSLionel Sambuc if (Ctx == Entity->getPrimaryContext())
1081f4a2713aSLionel Sambuc return S;
1082f4a2713aSLionel Sambuc }
1083f4a2713aSLionel Sambuc
1084*0a6a1f1dSLionel Sambuc return nullptr;
1085f4a2713aSLionel Sambuc }
1086f4a2713aSLionel Sambuc
1087f4a2713aSLionel Sambuc /// \brief Enter a new function scope
PushFunctionScope()1088f4a2713aSLionel Sambuc void Sema::PushFunctionScope() {
1089f4a2713aSLionel Sambuc if (FunctionScopes.size() == 1) {
1090f4a2713aSLionel Sambuc // Use the "top" function scope rather than having to allocate
1091f4a2713aSLionel Sambuc // memory for a new scope.
1092f4a2713aSLionel Sambuc FunctionScopes.back()->Clear();
1093f4a2713aSLionel Sambuc FunctionScopes.push_back(FunctionScopes.back());
1094f4a2713aSLionel Sambuc return;
1095f4a2713aSLionel Sambuc }
1096f4a2713aSLionel Sambuc
1097f4a2713aSLionel Sambuc FunctionScopes.push_back(new FunctionScopeInfo(getDiagnostics()));
1098f4a2713aSLionel Sambuc }
1099f4a2713aSLionel Sambuc
PushBlockScope(Scope * BlockScope,BlockDecl * Block)1100f4a2713aSLionel Sambuc void Sema::PushBlockScope(Scope *BlockScope, BlockDecl *Block) {
1101f4a2713aSLionel Sambuc FunctionScopes.push_back(new BlockScopeInfo(getDiagnostics(),
1102f4a2713aSLionel Sambuc BlockScope, Block));
1103f4a2713aSLionel Sambuc }
1104f4a2713aSLionel Sambuc
PushLambdaScope()1105f4a2713aSLionel Sambuc LambdaScopeInfo *Sema::PushLambdaScope() {
1106f4a2713aSLionel Sambuc LambdaScopeInfo *const LSI = new LambdaScopeInfo(getDiagnostics());
1107f4a2713aSLionel Sambuc FunctionScopes.push_back(LSI);
1108f4a2713aSLionel Sambuc return LSI;
1109f4a2713aSLionel Sambuc }
1110f4a2713aSLionel Sambuc
RecordParsingTemplateParameterDepth(unsigned Depth)1111f4a2713aSLionel Sambuc void Sema::RecordParsingTemplateParameterDepth(unsigned Depth) {
1112f4a2713aSLionel Sambuc if (LambdaScopeInfo *const LSI = getCurLambda()) {
1113f4a2713aSLionel Sambuc LSI->AutoTemplateParameterDepth = Depth;
1114f4a2713aSLionel Sambuc return;
1115f4a2713aSLionel Sambuc }
1116f4a2713aSLionel Sambuc llvm_unreachable(
1117f4a2713aSLionel Sambuc "Remove assertion if intentionally called in a non-lambda context.");
1118f4a2713aSLionel Sambuc }
1119f4a2713aSLionel Sambuc
PopFunctionScopeInfo(const AnalysisBasedWarnings::Policy * WP,const Decl * D,const BlockExpr * blkExpr)1120f4a2713aSLionel Sambuc void Sema::PopFunctionScopeInfo(const AnalysisBasedWarnings::Policy *WP,
1121f4a2713aSLionel Sambuc const Decl *D, const BlockExpr *blkExpr) {
1122f4a2713aSLionel Sambuc FunctionScopeInfo *Scope = FunctionScopes.pop_back_val();
1123f4a2713aSLionel Sambuc assert(!FunctionScopes.empty() && "mismatched push/pop!");
1124f4a2713aSLionel Sambuc
1125f4a2713aSLionel Sambuc // Issue any analysis-based warnings.
1126f4a2713aSLionel Sambuc if (WP && D)
1127f4a2713aSLionel Sambuc AnalysisWarnings.IssueWarnings(*WP, Scope, D, blkExpr);
1128*0a6a1f1dSLionel Sambuc else
1129*0a6a1f1dSLionel Sambuc for (const auto &PUD : Scope->PossiblyUnreachableDiags)
1130*0a6a1f1dSLionel Sambuc Diag(PUD.Loc, PUD.PD);
1131f4a2713aSLionel Sambuc
1132*0a6a1f1dSLionel Sambuc if (FunctionScopes.back() != Scope)
1133f4a2713aSLionel Sambuc delete Scope;
1134f4a2713aSLionel Sambuc }
1135f4a2713aSLionel Sambuc
PushCompoundScope()1136f4a2713aSLionel Sambuc void Sema::PushCompoundScope() {
1137f4a2713aSLionel Sambuc getCurFunction()->CompoundScopes.push_back(CompoundScopeInfo());
1138f4a2713aSLionel Sambuc }
1139f4a2713aSLionel Sambuc
PopCompoundScope()1140f4a2713aSLionel Sambuc void Sema::PopCompoundScope() {
1141f4a2713aSLionel Sambuc FunctionScopeInfo *CurFunction = getCurFunction();
1142f4a2713aSLionel Sambuc assert(!CurFunction->CompoundScopes.empty() && "mismatched push/pop");
1143f4a2713aSLionel Sambuc
1144f4a2713aSLionel Sambuc CurFunction->CompoundScopes.pop_back();
1145f4a2713aSLionel Sambuc }
1146f4a2713aSLionel Sambuc
1147f4a2713aSLionel Sambuc /// \brief Determine whether any errors occurred within this function/method/
1148f4a2713aSLionel Sambuc /// block.
hasAnyUnrecoverableErrorsInThisFunction() const1149f4a2713aSLionel Sambuc bool Sema::hasAnyUnrecoverableErrorsInThisFunction() const {
1150f4a2713aSLionel Sambuc return getCurFunction()->ErrorTrap.hasUnrecoverableErrorOccurred();
1151f4a2713aSLionel Sambuc }
1152f4a2713aSLionel Sambuc
getCurBlock()1153f4a2713aSLionel Sambuc BlockScopeInfo *Sema::getCurBlock() {
1154f4a2713aSLionel Sambuc if (FunctionScopes.empty())
1155*0a6a1f1dSLionel Sambuc return nullptr;
1156f4a2713aSLionel Sambuc
1157*0a6a1f1dSLionel Sambuc auto CurBSI = dyn_cast<BlockScopeInfo>(FunctionScopes.back());
1158*0a6a1f1dSLionel Sambuc if (CurBSI && CurBSI->TheDecl &&
1159*0a6a1f1dSLionel Sambuc !CurBSI->TheDecl->Encloses(CurContext)) {
1160*0a6a1f1dSLionel Sambuc // We have switched contexts due to template instantiation.
1161*0a6a1f1dSLionel Sambuc assert(!ActiveTemplateInstantiations.empty());
1162*0a6a1f1dSLionel Sambuc return nullptr;
1163*0a6a1f1dSLionel Sambuc }
1164*0a6a1f1dSLionel Sambuc
1165*0a6a1f1dSLionel Sambuc return CurBSI;
1166f4a2713aSLionel Sambuc }
1167f4a2713aSLionel Sambuc
getCurLambda()1168f4a2713aSLionel Sambuc LambdaScopeInfo *Sema::getCurLambda() {
1169f4a2713aSLionel Sambuc if (FunctionScopes.empty())
1170*0a6a1f1dSLionel Sambuc return nullptr;
1171f4a2713aSLionel Sambuc
1172*0a6a1f1dSLionel Sambuc auto CurLSI = dyn_cast<LambdaScopeInfo>(FunctionScopes.back());
1173*0a6a1f1dSLionel Sambuc if (CurLSI && CurLSI->Lambda &&
1174*0a6a1f1dSLionel Sambuc !CurLSI->Lambda->Encloses(CurContext)) {
1175*0a6a1f1dSLionel Sambuc // We have switched contexts due to template instantiation.
1176*0a6a1f1dSLionel Sambuc assert(!ActiveTemplateInstantiations.empty());
1177*0a6a1f1dSLionel Sambuc return nullptr;
1178*0a6a1f1dSLionel Sambuc }
1179*0a6a1f1dSLionel Sambuc
1180*0a6a1f1dSLionel Sambuc return CurLSI;
1181f4a2713aSLionel Sambuc }
1182f4a2713aSLionel Sambuc // We have a generic lambda if we parsed auto parameters, or we have
1183f4a2713aSLionel Sambuc // an associated template parameter list.
getCurGenericLambda()1184f4a2713aSLionel Sambuc LambdaScopeInfo *Sema::getCurGenericLambda() {
1185f4a2713aSLionel Sambuc if (LambdaScopeInfo *LSI = getCurLambda()) {
1186f4a2713aSLionel Sambuc return (LSI->AutoTemplateParams.size() ||
1187*0a6a1f1dSLionel Sambuc LSI->GLTemplateParameterList) ? LSI : nullptr;
1188f4a2713aSLionel Sambuc }
1189*0a6a1f1dSLionel Sambuc return nullptr;
1190f4a2713aSLionel Sambuc }
1191f4a2713aSLionel Sambuc
1192f4a2713aSLionel Sambuc
ActOnComment(SourceRange Comment)1193f4a2713aSLionel Sambuc void Sema::ActOnComment(SourceRange Comment) {
1194f4a2713aSLionel Sambuc if (!LangOpts.RetainCommentsFromSystemHeaders &&
1195f4a2713aSLionel Sambuc SourceMgr.isInSystemHeader(Comment.getBegin()))
1196f4a2713aSLionel Sambuc return;
1197f4a2713aSLionel Sambuc RawComment RC(SourceMgr, Comment, false,
1198f4a2713aSLionel Sambuc LangOpts.CommentOpts.ParseAllComments);
1199f4a2713aSLionel Sambuc if (RC.isAlmostTrailingComment()) {
1200f4a2713aSLionel Sambuc SourceRange MagicMarkerRange(Comment.getBegin(),
1201f4a2713aSLionel Sambuc Comment.getBegin().getLocWithOffset(3));
1202f4a2713aSLionel Sambuc StringRef MagicMarkerText;
1203f4a2713aSLionel Sambuc switch (RC.getKind()) {
1204f4a2713aSLionel Sambuc case RawComment::RCK_OrdinaryBCPL:
1205f4a2713aSLionel Sambuc MagicMarkerText = "///<";
1206f4a2713aSLionel Sambuc break;
1207f4a2713aSLionel Sambuc case RawComment::RCK_OrdinaryC:
1208f4a2713aSLionel Sambuc MagicMarkerText = "/**<";
1209f4a2713aSLionel Sambuc break;
1210f4a2713aSLionel Sambuc default:
1211f4a2713aSLionel Sambuc llvm_unreachable("if this is an almost Doxygen comment, "
1212f4a2713aSLionel Sambuc "it should be ordinary");
1213f4a2713aSLionel Sambuc }
1214f4a2713aSLionel Sambuc Diag(Comment.getBegin(), diag::warn_not_a_doxygen_trailing_member_comment) <<
1215f4a2713aSLionel Sambuc FixItHint::CreateReplacement(MagicMarkerRange, MagicMarkerText);
1216f4a2713aSLionel Sambuc }
1217f4a2713aSLionel Sambuc Context.addComment(RC);
1218f4a2713aSLionel Sambuc }
1219f4a2713aSLionel Sambuc
1220f4a2713aSLionel Sambuc // Pin this vtable to this file.
~ExternalSemaSource()1221f4a2713aSLionel Sambuc ExternalSemaSource::~ExternalSemaSource() {}
1222f4a2713aSLionel Sambuc
ReadMethodPool(Selector Sel)1223f4a2713aSLionel Sambuc void ExternalSemaSource::ReadMethodPool(Selector Sel) { }
1224f4a2713aSLionel Sambuc
ReadKnownNamespaces(SmallVectorImpl<NamespaceDecl * > & Namespaces)1225f4a2713aSLionel Sambuc void ExternalSemaSource::ReadKnownNamespaces(
1226f4a2713aSLionel Sambuc SmallVectorImpl<NamespaceDecl *> &Namespaces) {
1227f4a2713aSLionel Sambuc }
1228f4a2713aSLionel Sambuc
ReadUndefinedButUsed(llvm::DenseMap<NamedDecl *,SourceLocation> & Undefined)1229f4a2713aSLionel Sambuc void ExternalSemaSource::ReadUndefinedButUsed(
1230f4a2713aSLionel Sambuc llvm::DenseMap<NamedDecl *, SourceLocation> &Undefined) {
1231f4a2713aSLionel Sambuc }
1232f4a2713aSLionel Sambuc
print(raw_ostream & OS) const1233f4a2713aSLionel Sambuc void PrettyDeclStackTraceEntry::print(raw_ostream &OS) const {
1234f4a2713aSLionel Sambuc SourceLocation Loc = this->Loc;
1235f4a2713aSLionel Sambuc if (!Loc.isValid() && TheDecl) Loc = TheDecl->getLocation();
1236f4a2713aSLionel Sambuc if (Loc.isValid()) {
1237f4a2713aSLionel Sambuc Loc.print(OS, S.getSourceManager());
1238f4a2713aSLionel Sambuc OS << ": ";
1239f4a2713aSLionel Sambuc }
1240f4a2713aSLionel Sambuc OS << Message;
1241f4a2713aSLionel Sambuc
1242f4a2713aSLionel Sambuc if (TheDecl && isa<NamedDecl>(TheDecl)) {
1243f4a2713aSLionel Sambuc std::string Name = cast<NamedDecl>(TheDecl)->getNameAsString();
1244f4a2713aSLionel Sambuc if (!Name.empty())
1245f4a2713aSLionel Sambuc OS << " '" << Name << '\'';
1246f4a2713aSLionel Sambuc }
1247f4a2713aSLionel Sambuc
1248f4a2713aSLionel Sambuc OS << '\n';
1249f4a2713aSLionel Sambuc }
1250f4a2713aSLionel Sambuc
1251f4a2713aSLionel Sambuc /// \brief Figure out if an expression could be turned into a call.
1252f4a2713aSLionel Sambuc ///
1253f4a2713aSLionel Sambuc /// Use this when trying to recover from an error where the programmer may have
1254f4a2713aSLionel Sambuc /// written just the name of a function instead of actually calling it.
1255f4a2713aSLionel Sambuc ///
1256f4a2713aSLionel Sambuc /// \param E - The expression to examine.
1257f4a2713aSLionel Sambuc /// \param ZeroArgCallReturnTy - If the expression can be turned into a call
1258f4a2713aSLionel Sambuc /// with no arguments, this parameter is set to the type returned by such a
1259f4a2713aSLionel Sambuc /// call; otherwise, it is set to an empty QualType.
1260f4a2713aSLionel Sambuc /// \param OverloadSet - If the expression is an overloaded function
1261f4a2713aSLionel Sambuc /// name, this parameter is populated with the decls of the various overloads.
tryExprAsCall(Expr & E,QualType & ZeroArgCallReturnTy,UnresolvedSetImpl & OverloadSet)1262f4a2713aSLionel Sambuc bool Sema::tryExprAsCall(Expr &E, QualType &ZeroArgCallReturnTy,
1263f4a2713aSLionel Sambuc UnresolvedSetImpl &OverloadSet) {
1264f4a2713aSLionel Sambuc ZeroArgCallReturnTy = QualType();
1265f4a2713aSLionel Sambuc OverloadSet.clear();
1266f4a2713aSLionel Sambuc
1267*0a6a1f1dSLionel Sambuc const OverloadExpr *Overloads = nullptr;
1268f4a2713aSLionel Sambuc bool IsMemExpr = false;
1269f4a2713aSLionel Sambuc if (E.getType() == Context.OverloadTy) {
1270f4a2713aSLionel Sambuc OverloadExpr::FindResult FR = OverloadExpr::find(const_cast<Expr*>(&E));
1271f4a2713aSLionel Sambuc
1272f4a2713aSLionel Sambuc // Ignore overloads that are pointer-to-member constants.
1273f4a2713aSLionel Sambuc if (FR.HasFormOfMemberPointer)
1274f4a2713aSLionel Sambuc return false;
1275f4a2713aSLionel Sambuc
1276f4a2713aSLionel Sambuc Overloads = FR.Expression;
1277f4a2713aSLionel Sambuc } else if (E.getType() == Context.BoundMemberTy) {
1278f4a2713aSLionel Sambuc Overloads = dyn_cast<UnresolvedMemberExpr>(E.IgnoreParens());
1279f4a2713aSLionel Sambuc IsMemExpr = true;
1280f4a2713aSLionel Sambuc }
1281f4a2713aSLionel Sambuc
1282f4a2713aSLionel Sambuc bool Ambiguous = false;
1283f4a2713aSLionel Sambuc
1284f4a2713aSLionel Sambuc if (Overloads) {
1285f4a2713aSLionel Sambuc for (OverloadExpr::decls_iterator it = Overloads->decls_begin(),
1286f4a2713aSLionel Sambuc DeclsEnd = Overloads->decls_end(); it != DeclsEnd; ++it) {
1287f4a2713aSLionel Sambuc OverloadSet.addDecl(*it);
1288f4a2713aSLionel Sambuc
1289f4a2713aSLionel Sambuc // Check whether the function is a non-template, non-member which takes no
1290f4a2713aSLionel Sambuc // arguments.
1291f4a2713aSLionel Sambuc if (IsMemExpr)
1292f4a2713aSLionel Sambuc continue;
1293f4a2713aSLionel Sambuc if (const FunctionDecl *OverloadDecl
1294f4a2713aSLionel Sambuc = dyn_cast<FunctionDecl>((*it)->getUnderlyingDecl())) {
1295f4a2713aSLionel Sambuc if (OverloadDecl->getMinRequiredArguments() == 0) {
1296f4a2713aSLionel Sambuc if (!ZeroArgCallReturnTy.isNull() && !Ambiguous) {
1297f4a2713aSLionel Sambuc ZeroArgCallReturnTy = QualType();
1298f4a2713aSLionel Sambuc Ambiguous = true;
1299f4a2713aSLionel Sambuc } else
1300*0a6a1f1dSLionel Sambuc ZeroArgCallReturnTy = OverloadDecl->getReturnType();
1301f4a2713aSLionel Sambuc }
1302f4a2713aSLionel Sambuc }
1303f4a2713aSLionel Sambuc }
1304f4a2713aSLionel Sambuc
1305f4a2713aSLionel Sambuc // If it's not a member, use better machinery to try to resolve the call
1306f4a2713aSLionel Sambuc if (!IsMemExpr)
1307f4a2713aSLionel Sambuc return !ZeroArgCallReturnTy.isNull();
1308f4a2713aSLionel Sambuc }
1309f4a2713aSLionel Sambuc
1310f4a2713aSLionel Sambuc // Attempt to call the member with no arguments - this will correctly handle
1311f4a2713aSLionel Sambuc // member templates with defaults/deduction of template arguments, overloads
1312f4a2713aSLionel Sambuc // with default arguments, etc.
1313f4a2713aSLionel Sambuc if (IsMemExpr && !E.isTypeDependent()) {
1314f4a2713aSLionel Sambuc bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
1315f4a2713aSLionel Sambuc getDiagnostics().setSuppressAllDiagnostics(true);
1316*0a6a1f1dSLionel Sambuc ExprResult R = BuildCallToMemberFunction(nullptr, &E, SourceLocation(),
1317*0a6a1f1dSLionel Sambuc None, SourceLocation());
1318f4a2713aSLionel Sambuc getDiagnostics().setSuppressAllDiagnostics(Suppress);
1319f4a2713aSLionel Sambuc if (R.isUsable()) {
1320f4a2713aSLionel Sambuc ZeroArgCallReturnTy = R.get()->getType();
1321f4a2713aSLionel Sambuc return true;
1322f4a2713aSLionel Sambuc }
1323f4a2713aSLionel Sambuc return false;
1324f4a2713aSLionel Sambuc }
1325f4a2713aSLionel Sambuc
1326f4a2713aSLionel Sambuc if (const DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E.IgnoreParens())) {
1327f4a2713aSLionel Sambuc if (const FunctionDecl *Fun = dyn_cast<FunctionDecl>(DeclRef->getDecl())) {
1328f4a2713aSLionel Sambuc if (Fun->getMinRequiredArguments() == 0)
1329*0a6a1f1dSLionel Sambuc ZeroArgCallReturnTy = Fun->getReturnType();
1330f4a2713aSLionel Sambuc return true;
1331f4a2713aSLionel Sambuc }
1332f4a2713aSLionel Sambuc }
1333f4a2713aSLionel Sambuc
1334f4a2713aSLionel Sambuc // We don't have an expression that's convenient to get a FunctionDecl from,
1335f4a2713aSLionel Sambuc // but we can at least check if the type is "function of 0 arguments".
1336f4a2713aSLionel Sambuc QualType ExprTy = E.getType();
1337*0a6a1f1dSLionel Sambuc const FunctionType *FunTy = nullptr;
1338f4a2713aSLionel Sambuc QualType PointeeTy = ExprTy->getPointeeType();
1339f4a2713aSLionel Sambuc if (!PointeeTy.isNull())
1340f4a2713aSLionel Sambuc FunTy = PointeeTy->getAs<FunctionType>();
1341f4a2713aSLionel Sambuc if (!FunTy)
1342f4a2713aSLionel Sambuc FunTy = ExprTy->getAs<FunctionType>();
1343f4a2713aSLionel Sambuc
1344f4a2713aSLionel Sambuc if (const FunctionProtoType *FPT =
1345f4a2713aSLionel Sambuc dyn_cast_or_null<FunctionProtoType>(FunTy)) {
1346*0a6a1f1dSLionel Sambuc if (FPT->getNumParams() == 0)
1347*0a6a1f1dSLionel Sambuc ZeroArgCallReturnTy = FunTy->getReturnType();
1348f4a2713aSLionel Sambuc return true;
1349f4a2713aSLionel Sambuc }
1350f4a2713aSLionel Sambuc return false;
1351f4a2713aSLionel Sambuc }
1352f4a2713aSLionel Sambuc
1353f4a2713aSLionel Sambuc /// \brief Give notes for a set of overloads.
1354f4a2713aSLionel Sambuc ///
1355f4a2713aSLionel Sambuc /// A companion to tryExprAsCall. In cases when the name that the programmer
1356f4a2713aSLionel Sambuc /// wrote was an overloaded function, we may be able to make some guesses about
1357f4a2713aSLionel Sambuc /// plausible overloads based on their return types; such guesses can be handed
1358f4a2713aSLionel Sambuc /// off to this method to be emitted as notes.
1359f4a2713aSLionel Sambuc ///
1360f4a2713aSLionel Sambuc /// \param Overloads - The overloads to note.
1361f4a2713aSLionel Sambuc /// \param FinalNoteLoc - If we've suppressed printing some overloads due to
1362f4a2713aSLionel Sambuc /// -fshow-overloads=best, this is the location to attach to the note about too
1363f4a2713aSLionel Sambuc /// many candidates. Typically this will be the location of the original
1364f4a2713aSLionel Sambuc /// ill-formed expression.
noteOverloads(Sema & S,const UnresolvedSetImpl & Overloads,const SourceLocation FinalNoteLoc)1365f4a2713aSLionel Sambuc static void noteOverloads(Sema &S, const UnresolvedSetImpl &Overloads,
1366f4a2713aSLionel Sambuc const SourceLocation FinalNoteLoc) {
1367f4a2713aSLionel Sambuc int ShownOverloads = 0;
1368f4a2713aSLionel Sambuc int SuppressedOverloads = 0;
1369f4a2713aSLionel Sambuc for (UnresolvedSetImpl::iterator It = Overloads.begin(),
1370f4a2713aSLionel Sambuc DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) {
1371f4a2713aSLionel Sambuc // FIXME: Magic number for max shown overloads stolen from
1372f4a2713aSLionel Sambuc // OverloadCandidateSet::NoteCandidates.
1373f4a2713aSLionel Sambuc if (ShownOverloads >= 4 && S.Diags.getShowOverloads() == Ovl_Best) {
1374f4a2713aSLionel Sambuc ++SuppressedOverloads;
1375f4a2713aSLionel Sambuc continue;
1376f4a2713aSLionel Sambuc }
1377f4a2713aSLionel Sambuc
1378f4a2713aSLionel Sambuc NamedDecl *Fn = (*It)->getUnderlyingDecl();
1379f4a2713aSLionel Sambuc S.Diag(Fn->getLocation(), diag::note_possible_target_of_call);
1380f4a2713aSLionel Sambuc ++ShownOverloads;
1381f4a2713aSLionel Sambuc }
1382f4a2713aSLionel Sambuc
1383f4a2713aSLionel Sambuc if (SuppressedOverloads)
1384f4a2713aSLionel Sambuc S.Diag(FinalNoteLoc, diag::note_ovl_too_many_candidates)
1385f4a2713aSLionel Sambuc << SuppressedOverloads;
1386f4a2713aSLionel Sambuc }
1387f4a2713aSLionel Sambuc
notePlausibleOverloads(Sema & S,SourceLocation Loc,const UnresolvedSetImpl & Overloads,bool (* IsPlausibleResult)(QualType))1388f4a2713aSLionel Sambuc static void notePlausibleOverloads(Sema &S, SourceLocation Loc,
1389f4a2713aSLionel Sambuc const UnresolvedSetImpl &Overloads,
1390f4a2713aSLionel Sambuc bool (*IsPlausibleResult)(QualType)) {
1391f4a2713aSLionel Sambuc if (!IsPlausibleResult)
1392f4a2713aSLionel Sambuc return noteOverloads(S, Overloads, Loc);
1393f4a2713aSLionel Sambuc
1394f4a2713aSLionel Sambuc UnresolvedSet<2> PlausibleOverloads;
1395f4a2713aSLionel Sambuc for (OverloadExpr::decls_iterator It = Overloads.begin(),
1396f4a2713aSLionel Sambuc DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) {
1397f4a2713aSLionel Sambuc const FunctionDecl *OverloadDecl = cast<FunctionDecl>(*It);
1398*0a6a1f1dSLionel Sambuc QualType OverloadResultTy = OverloadDecl->getReturnType();
1399f4a2713aSLionel Sambuc if (IsPlausibleResult(OverloadResultTy))
1400f4a2713aSLionel Sambuc PlausibleOverloads.addDecl(It.getDecl());
1401f4a2713aSLionel Sambuc }
1402f4a2713aSLionel Sambuc noteOverloads(S, PlausibleOverloads, Loc);
1403f4a2713aSLionel Sambuc }
1404f4a2713aSLionel Sambuc
1405f4a2713aSLionel Sambuc /// Determine whether the given expression can be called by just
1406f4a2713aSLionel Sambuc /// putting parentheses after it. Notably, expressions with unary
1407f4a2713aSLionel Sambuc /// operators can't be because the unary operator will start parsing
1408f4a2713aSLionel Sambuc /// outside the call.
IsCallableWithAppend(Expr * E)1409f4a2713aSLionel Sambuc static bool IsCallableWithAppend(Expr *E) {
1410f4a2713aSLionel Sambuc E = E->IgnoreImplicit();
1411f4a2713aSLionel Sambuc return (!isa<CStyleCastExpr>(E) &&
1412f4a2713aSLionel Sambuc !isa<UnaryOperator>(E) &&
1413f4a2713aSLionel Sambuc !isa<BinaryOperator>(E) &&
1414f4a2713aSLionel Sambuc !isa<CXXOperatorCallExpr>(E));
1415f4a2713aSLionel Sambuc }
1416f4a2713aSLionel Sambuc
tryToRecoverWithCall(ExprResult & E,const PartialDiagnostic & PD,bool ForceComplain,bool (* IsPlausibleResult)(QualType))1417f4a2713aSLionel Sambuc bool Sema::tryToRecoverWithCall(ExprResult &E, const PartialDiagnostic &PD,
1418f4a2713aSLionel Sambuc bool ForceComplain,
1419f4a2713aSLionel Sambuc bool (*IsPlausibleResult)(QualType)) {
1420f4a2713aSLionel Sambuc SourceLocation Loc = E.get()->getExprLoc();
1421f4a2713aSLionel Sambuc SourceRange Range = E.get()->getSourceRange();
1422f4a2713aSLionel Sambuc
1423f4a2713aSLionel Sambuc QualType ZeroArgCallTy;
1424f4a2713aSLionel Sambuc UnresolvedSet<4> Overloads;
1425f4a2713aSLionel Sambuc if (tryExprAsCall(*E.get(), ZeroArgCallTy, Overloads) &&
1426f4a2713aSLionel Sambuc !ZeroArgCallTy.isNull() &&
1427f4a2713aSLionel Sambuc (!IsPlausibleResult || IsPlausibleResult(ZeroArgCallTy))) {
1428f4a2713aSLionel Sambuc // At this point, we know E is potentially callable with 0
1429f4a2713aSLionel Sambuc // arguments and that it returns something of a reasonable type,
1430f4a2713aSLionel Sambuc // so we can emit a fixit and carry on pretending that E was
1431f4a2713aSLionel Sambuc // actually a CallExpr.
1432f4a2713aSLionel Sambuc SourceLocation ParenInsertionLoc = PP.getLocForEndOfToken(Range.getEnd());
1433f4a2713aSLionel Sambuc Diag(Loc, PD)
1434f4a2713aSLionel Sambuc << /*zero-arg*/ 1 << Range
1435f4a2713aSLionel Sambuc << (IsCallableWithAppend(E.get())
1436f4a2713aSLionel Sambuc ? FixItHint::CreateInsertion(ParenInsertionLoc, "()")
1437f4a2713aSLionel Sambuc : FixItHint());
1438f4a2713aSLionel Sambuc notePlausibleOverloads(*this, Loc, Overloads, IsPlausibleResult);
1439f4a2713aSLionel Sambuc
1440f4a2713aSLionel Sambuc // FIXME: Try this before emitting the fixit, and suppress diagnostics
1441f4a2713aSLionel Sambuc // while doing so.
1442*0a6a1f1dSLionel Sambuc E = ActOnCallExpr(nullptr, E.get(), Range.getEnd(), None,
1443f4a2713aSLionel Sambuc Range.getEnd().getLocWithOffset(1));
1444f4a2713aSLionel Sambuc return true;
1445f4a2713aSLionel Sambuc }
1446f4a2713aSLionel Sambuc
1447f4a2713aSLionel Sambuc if (!ForceComplain) return false;
1448f4a2713aSLionel Sambuc
1449f4a2713aSLionel Sambuc Diag(Loc, PD) << /*not zero-arg*/ 0 << Range;
1450f4a2713aSLionel Sambuc notePlausibleOverloads(*this, Loc, Overloads, IsPlausibleResult);
1451f4a2713aSLionel Sambuc E = ExprError();
1452f4a2713aSLionel Sambuc return true;
1453f4a2713aSLionel Sambuc }
1454f4a2713aSLionel Sambuc
getSuperIdentifier() const1455f4a2713aSLionel Sambuc IdentifierInfo *Sema::getSuperIdentifier() const {
1456f4a2713aSLionel Sambuc if (!Ident_super)
1457f4a2713aSLionel Sambuc Ident_super = &Context.Idents.get("super");
1458f4a2713aSLionel Sambuc return Ident_super;
1459f4a2713aSLionel Sambuc }
1460f4a2713aSLionel Sambuc
getFloat128Identifier() const1461f4a2713aSLionel Sambuc IdentifierInfo *Sema::getFloat128Identifier() const {
1462f4a2713aSLionel Sambuc if (!Ident___float128)
1463f4a2713aSLionel Sambuc Ident___float128 = &Context.Idents.get("__float128");
1464f4a2713aSLionel Sambuc return Ident___float128;
1465f4a2713aSLionel Sambuc }
1466f4a2713aSLionel Sambuc
PushCapturedRegionScope(Scope * S,CapturedDecl * CD,RecordDecl * RD,CapturedRegionKind K)1467f4a2713aSLionel Sambuc void Sema::PushCapturedRegionScope(Scope *S, CapturedDecl *CD, RecordDecl *RD,
1468f4a2713aSLionel Sambuc CapturedRegionKind K) {
1469*0a6a1f1dSLionel Sambuc CapturingScopeInfo *CSI = new CapturedRegionScopeInfo(
1470*0a6a1f1dSLionel Sambuc getDiagnostics(), S, CD, RD, CD->getContextParam(), K);
1471f4a2713aSLionel Sambuc CSI->ReturnType = Context.VoidTy;
1472f4a2713aSLionel Sambuc FunctionScopes.push_back(CSI);
1473f4a2713aSLionel Sambuc }
1474f4a2713aSLionel Sambuc
getCurCapturedRegion()1475f4a2713aSLionel Sambuc CapturedRegionScopeInfo *Sema::getCurCapturedRegion() {
1476f4a2713aSLionel Sambuc if (FunctionScopes.empty())
1477*0a6a1f1dSLionel Sambuc return nullptr;
1478f4a2713aSLionel Sambuc
1479f4a2713aSLionel Sambuc return dyn_cast<CapturedRegionScopeInfo>(FunctionScopes.back());
1480f4a2713aSLionel Sambuc }
1481