Chris Lattner | ddd6fc8 | 2006-11-10 04:58:55 +0000 | [diff] [blame] | 1 | //===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===// |
Chris Lattner | 3e7bd4e | 2006-08-17 05:51:27 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | 3e7bd4e | 2006-08-17 05:51:27 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Chris Lattner | ddd6fc8 | 2006-11-10 04:58:55 +0000 | [diff] [blame] | 9 | // This file implements the actions class which performs semantic analysis and |
| 10 | // builds an AST out of a parse stream. |
Chris Lattner | 3e7bd4e | 2006-08-17 05:51:27 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | cb6a382 | 2006-11-10 06:20:45 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 639cccc | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTDiagnostic.h" |
John McCall | 28a0cf7 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclCXX.h" |
Daniel Jasper | 0baec549 | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclFriend.h" |
Daniel Dunbar | 6e8aa53 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 21 | #include "clang/AST/PrettyDeclStackTrace.h" |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 22 | #include "clang/AST/StmtCXX.h" |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 23 | #include "clang/Basic/DiagnosticOptions.h" |
Anders Carlsson | f68079e | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 24 | #include "clang/Basic/PartialDiagnostic.h" |
Richard Smith | 26a92d5 | 2019-08-26 18:18:07 +0000 | [diff] [blame] | 25 | #include "clang/Basic/Stack.h" |
Chris Lattner | 7d4f5c4 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 26 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 27 | #include "clang/Lex/HeaderSearch.h" |
| 28 | #include "clang/Lex/Preprocessor.h" |
| 29 | #include "clang/Sema/CXXFieldCollector.h" |
| 30 | #include "clang/Sema/DelayedDiagnostic.h" |
| 31 | #include "clang/Sema/ExternalSemaSource.h" |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 32 | #include "clang/Sema/Initialization.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 33 | #include "clang/Sema/MultiplexExternalSemaSource.h" |
| 34 | #include "clang/Sema/ObjCMethodList.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 35 | #include "clang/Sema/Scope.h" |
| 36 | #include "clang/Sema/ScopeInfo.h" |
| 37 | #include "clang/Sema/SemaConsumer.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 38 | #include "clang/Sema/SemaInternal.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 39 | #include "clang/Sema/TemplateDeduction.h" |
Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 40 | #include "clang/Sema/TemplateInstCallback.h" |
Ilya Biryukov | aeae71c | 2019-10-09 10:00:05 +0000 | [diff] [blame] | 41 | #include "clang/Sema/TypoCorrection.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/DenseMap.h" |
| 43 | #include "llvm/ADT/SmallSet.h" |
Anton Afanasyev | d880de2 | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 44 | #include "llvm/Support/TimeProfiler.h" |
| 45 | |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 46 | using namespace clang; |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 47 | using namespace sema; |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 48 | |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 49 | SourceLocation Sema::getLocForEndOfToken(SourceLocation Loc, unsigned Offset) { |
| 50 | return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, LangOpts); |
| 51 | } |
| 52 | |
| 53 | ModuleLoader &Sema::getModuleLoader() const { return PP.getModuleLoader(); } |
| 54 | |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 55 | PrintingPolicy Sema::getPrintingPolicy(const ASTContext &Context, |
| 56 | const Preprocessor &PP) { |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 57 | PrintingPolicy Policy = Context.getPrintingPolicy(); |
Joel E. Denny | 7bcc210 | 2018-05-14 18:41:44 +0000 | [diff] [blame] | 58 | // In diagnostics, we print _Bool as bool if the latter is defined as the |
| 59 | // former. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 60 | Policy.Bool = Context.getLangOpts().Bool; |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 61 | if (!Policy.Bool) { |
Richard Smith | 301bc21 | 2016-05-19 01:39:10 +0000 | [diff] [blame] | 62 | if (const MacroInfo *BoolMacro = PP.getMacroInfo(Context.getBoolName())) { |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 63 | Policy.Bool = BoolMacro->isObjectLike() && |
Richard Smith | 301bc21 | 2016-05-19 01:39:10 +0000 | [diff] [blame] | 64 | BoolMacro->getNumTokens() == 1 && |
| 65 | BoolMacro->getReplacementToken(0).is(tok::kw__Bool); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 68 | |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 69 | return Policy; |
| 70 | } |
| 71 | |
Douglas Gregor | f11096c | 2010-08-25 18:07:12 +0000 | [diff] [blame] | 72 | void Sema::ActOnTranslationUnitScope(Scope *S) { |
Steve Naroff | c62adb6 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 73 | TUScope = S; |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 74 | PushDeclContext(S, Context.getTranslationUnitDecl()); |
Steve Naroff | 7f549f1 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Alex Lorenz | 45b4014 | 2017-07-28 14:41:21 +0000 | [diff] [blame] | 77 | namespace clang { |
| 78 | namespace sema { |
| 79 | |
| 80 | class SemaPPCallbacks : public PPCallbacks { |
| 81 | Sema *S = nullptr; |
| 82 | llvm::SmallVector<SourceLocation, 8> IncludeStack; |
| 83 | |
| 84 | public: |
| 85 | void set(Sema &S) { this->S = &S; } |
| 86 | |
| 87 | void reset() { S = nullptr; } |
| 88 | |
| 89 | virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
| 90 | SrcMgr::CharacteristicKind FileType, |
| 91 | FileID PrevFID) override { |
| 92 | if (!S) |
| 93 | return; |
| 94 | switch (Reason) { |
| 95 | case EnterFile: { |
| 96 | SourceManager &SM = S->getSourceManager(); |
| 97 | SourceLocation IncludeLoc = SM.getIncludeLoc(SM.getFileID(Loc)); |
| 98 | if (IncludeLoc.isValid()) { |
Anton Afanasyev | d880de2 | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 99 | if (llvm::timeTraceProfilerEnabled()) { |
| 100 | const FileEntry *FE = SM.getFileEntryForID(SM.getFileID(Loc)); |
| 101 | llvm::timeTraceProfilerBegin( |
| 102 | "Source", FE != nullptr ? FE->getName() : StringRef("<unknown>")); |
| 103 | } |
| 104 | |
Alex Lorenz | 45b4014 | 2017-07-28 14:41:21 +0000 | [diff] [blame] | 105 | IncludeStack.push_back(IncludeLoc); |
| 106 | S->DiagnoseNonDefaultPragmaPack( |
| 107 | Sema::PragmaPackDiagnoseKind::NonDefaultStateAtInclude, IncludeLoc); |
| 108 | } |
| 109 | break; |
| 110 | } |
| 111 | case ExitFile: |
Anton Afanasyev | d880de2 | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 112 | if (!IncludeStack.empty()) { |
| 113 | if (llvm::timeTraceProfilerEnabled()) |
| 114 | llvm::timeTraceProfilerEnd(); |
| 115 | |
Alex Lorenz | 45b4014 | 2017-07-28 14:41:21 +0000 | [diff] [blame] | 116 | S->DiagnoseNonDefaultPragmaPack( |
| 117 | Sema::PragmaPackDiagnoseKind::ChangedStateAtExit, |
| 118 | IncludeStack.pop_back_val()); |
Anton Afanasyev | d880de2 | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 119 | } |
Alex Lorenz | 45b4014 | 2017-07-28 14:41:21 +0000 | [diff] [blame] | 120 | break; |
| 121 | default: |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | } // end namespace sema |
| 128 | } // end namespace clang |
| 129 | |
Douglas Gregor | 54feb84 | 2009-04-14 16:27:31 +0000 | [diff] [blame] | 130 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer, |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 131 | TranslationUnitKind TUKind, CodeCompleteConsumer *CodeCompleter) |
| 132 | : ExternalSource(nullptr), isMultiplexExternalSource(false), |
| 133 | FPFeatures(pp.getLangOpts()), LangOpts(pp.getLangOpts()), PP(pp), |
| 134 | Context(ctxt), Consumer(consumer), Diags(PP.getDiagnostics()), |
| 135 | SourceMgr(PP.getSourceManager()), CollectStats(false), |
| 136 | CodeCompleter(CodeCompleter), CurContext(nullptr), |
| 137 | OriginalLexicalContext(nullptr), MSStructPragmaOn(false), |
| 138 | MSPointerToMemberRepresentationMethod( |
| 139 | LangOpts.getMSPointerToMemberRepresentationMethod()), |
Reid Kleckner | 2692eb0 | 2019-11-22 14:55:49 -0800 | [diff] [blame] | 140 | VtorDispStack(LangOpts.getVtorDispMode()), PackStack(0), |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 141 | DataSegStack(nullptr), BSSSegStack(nullptr), ConstSegStack(nullptr), |
| 142 | CodeSegStack(nullptr), CurInitSeg(nullptr), VisContext(nullptr), |
| 143 | PragmaAttributeCurrentTargetDecl(nullptr), |
| 144 | IsBuildingRecoveryCallExpr(false), Cleanup{}, LateTemplateParser(nullptr), |
| 145 | LateTemplateParserCleanup(nullptr), OpaqueParser(nullptr), IdResolver(pp), |
| 146 | StdExperimentalNamespaceCache(nullptr), StdInitializerList(nullptr), |
Brian Gesiak | 3e65d9a | 2018-07-14 18:21:44 +0000 | [diff] [blame] | 147 | StdCoroutineTraitsCache(nullptr), CXXTypeInfoDecl(nullptr), |
| 148 | MSVCGuidDecl(nullptr), NSNumberDecl(nullptr), NSValueDecl(nullptr), |
| 149 | NSStringDecl(nullptr), StringWithUTF8StringMethod(nullptr), |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 150 | ValueWithBytesObjCTypeMethod(nullptr), NSArrayDecl(nullptr), |
| 151 | ArrayWithObjectsMethod(nullptr), NSDictionaryDecl(nullptr), |
| 152 | DictionaryWithObjectsMethod(nullptr), GlobalNewDeleteDeclared(false), |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 153 | TUKind(TUKind), NumSFINAEErrors(0), |
| 154 | FullyCheckedComparisonCategories( |
| 155 | static_cast<unsigned>(ComparisonCategoryType::Last) + 1), |
| 156 | AccessCheckingSFINAE(false), InNonInstantiationSFINAEContext(false), |
| 157 | NonInstantiationEntries(0), ArgumentPackSubstitutionIndex(-1), |
| 158 | CurrentInstantiationScope(nullptr), DisableTypoCorrection(false), |
| 159 | TyposCorrected(0), AnalysisWarnings(*this), |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 160 | ThreadSafetyDeclCache(nullptr), VarDataSharingAttributesStack(nullptr), |
| 161 | CurScope(nullptr), Ident_super(nullptr), Ident___float128(nullptr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 162 | TUScope = nullptr; |
Gauthier Harnisch | 0bb4d46 | 2019-06-15 08:32:56 +0000 | [diff] [blame] | 163 | isConstantEvaluatedOverride = false; |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 164 | |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 165 | LoadedExternalKnownNamespaces = false; |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 166 | for (unsigned I = 0; I != NSAPI::NumNSNumberLiteralMethods; ++I) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 167 | NSNumberLiteralMethods[I] = nullptr; |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 168 | |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 169 | if (getLangOpts().ObjC) |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 170 | NSAPIObj.reset(new NSAPI(Context)); |
| 171 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 172 | if (getLangOpts().CPlusPlus) |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 173 | FieldCollector.reset(new CXXFieldCollector()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | |
Chris Lattner | 6a2ed6f | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 175 | // Tell diagnostics how to render things from the AST library. |
Craig Topper | 1212626 | 2015-11-15 17:27:57 +0000 | [diff] [blame] | 176 | Diags.SetArgToStringFn(&FormatASTNodeDiagnosticArgument, &Context); |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 177 | |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 178 | ExprEvalContexts.emplace_back( |
| 179 | ExpressionEvaluationContext::PotentiallyEvaluated, 0, CleanupInfo{}, |
Nicolas Lesser | b6d5c58 | 2018-07-12 18:45:41 +0000 | [diff] [blame] | 180 | nullptr, ExpressionEvaluationContextRecord::EK_Other); |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 181 | |
Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 182 | // Initialization of data sharing attributes stack for OpenMP |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 183 | InitDataSharingAttributesStack(); |
Alex Lorenz | 45b4014 | 2017-07-28 14:41:21 +0000 | [diff] [blame] | 184 | |
| 185 | std::unique_ptr<sema::SemaPPCallbacks> Callbacks = |
Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 186 | std::make_unique<sema::SemaPPCallbacks>(); |
Alex Lorenz | 45b4014 | 2017-07-28 14:41:21 +0000 | [diff] [blame] | 187 | SemaPPCallbackHandler = Callbacks.get(); |
| 188 | PP.addPPCallbacks(std::move(Callbacks)); |
| 189 | SemaPPCallbackHandler->set(*this); |
Douglas Gregor | 5c6f10b | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Reid Kleckner | 586f65d | 2019-11-15 11:22:02 -0800 | [diff] [blame] | 192 | // Anchor Sema's type info to this TU. |
| 193 | void Sema::anchor() {} |
| 194 | |
Alp Toker | f22856a | 2013-12-18 15:29:05 +0000 | [diff] [blame] | 195 | void Sema::addImplicitTypedef(StringRef Name, QualType T) { |
| 196 | DeclarationName DN = &Context.Idents.get(Name); |
| 197 | if (IdResolver.begin(DN) == IdResolver.end()) |
| 198 | PushOnScopeChains(Context.buildImplicitTypedef(T, Name), TUScope); |
| 199 | } |
| 200 | |
Douglas Gregor | 5c6f10b | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 201 | void Sema::Initialize() { |
Douglas Gregor | 5c6f10b | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 202 | if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer)) |
| 203 | SC->InitializeSema(*this); |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 204 | |
Douglas Gregor | 5c6f10b | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 205 | // Tell the external Sema source about this Sema object. |
| 206 | if (ExternalSemaSource *ExternalSema |
| 207 | = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource())) |
| 208 | ExternalSema->InitializeSema(*this); |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 209 | |
Ben Langmuir | bb1c918 | 2014-09-05 20:24:27 +0000 | [diff] [blame] | 210 | // This needs to happen after ExternalSemaSource::InitializeSema(this) or we |
| 211 | // will not be able to merge any duplicate __va_list_tag decls correctly. |
| 212 | VAListTagName = PP.getIdentifierInfo("__va_list_tag"); |
| 213 | |
Richard Smith | 33e0f7e | 2015-07-22 02:08:40 +0000 | [diff] [blame] | 214 | if (!TUScope) |
| 215 | return; |
| 216 | |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 217 | // Initialize predefined 128-bit integer types, if needed. |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 218 | if (Context.getTargetInfo().hasInt128Type()) { |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 219 | // If either of the 128-bit integer types are unavailable to name lookup, |
| 220 | // define them now. |
| 221 | DeclarationName Int128 = &Context.Idents.get("__int128_t"); |
Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 222 | if (IdResolver.begin(Int128) == IdResolver.end()) |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 223 | PushOnScopeChains(Context.getInt128Decl(), TUScope); |
| 224 | |
| 225 | DeclarationName UInt128 = &Context.Idents.get("__uint128_t"); |
Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 226 | if (IdResolver.begin(UInt128) == IdResolver.end()) |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 227 | PushOnScopeChains(Context.getUInt128Decl(), TUScope); |
| 228 | } |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 229 | |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 230 | |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 231 | // Initialize predefined Objective-C types: |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 232 | if (getLangOpts().ObjC) { |
Douglas Gregor | 52e0280 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 233 | // If 'SEL' does not yet refer to any declarations, make it refer to the |
| 234 | // predefined 'SEL'. |
| 235 | DeclarationName SEL = &Context.Idents.get("SEL"); |
Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 236 | if (IdResolver.begin(SEL) == IdResolver.end()) |
Douglas Gregor | 52e0280 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 237 | PushOnScopeChains(Context.getObjCSelDecl(), TUScope); |
| 238 | |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 239 | // If 'id' does not yet refer to any declarations, make it refer to the |
| 240 | // predefined 'id'. |
| 241 | DeclarationName Id = &Context.Idents.get("id"); |
Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 242 | if (IdResolver.begin(Id) == IdResolver.end()) |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 243 | PushOnScopeChains(Context.getObjCIdDecl(), TUScope); |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 244 | |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 245 | // Create the built-in typedef for 'Class'. |
| 246 | DeclarationName Class = &Context.Idents.get("Class"); |
Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 247 | if (IdResolver.begin(Class) == IdResolver.end()) |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 248 | PushOnScopeChains(Context.getObjCClassDecl(), TUScope); |
Douglas Gregor | d53ae83 | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 249 | |
| 250 | // Create the built-in forward declaratino for 'Protocol'. |
| 251 | DeclarationName Protocol = &Context.Idents.get("Protocol"); |
| 252 | if (IdResolver.begin(Protocol) == IdResolver.end()) |
| 253 | PushOnScopeChains(Context.getObjCProtocolDecl(), TUScope); |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 254 | } |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 255 | |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 256 | // Create the internal type for the *StringMakeConstantString builtins. |
| 257 | DeclarationName ConstantString = &Context.Idents.get("__NSConstantString"); |
| 258 | if (IdResolver.begin(ConstantString) == IdResolver.end()) |
| 259 | PushOnScopeChains(Context.getCFConstantStringDecl(), TUScope); |
| 260 | |
Alp Toker | e1fab52 | 2014-01-04 15:25:02 +0000 | [diff] [blame] | 261 | // Initialize Microsoft "predefined C++ types". |
Craig Topper | 1212626 | 2015-11-15 17:27:57 +0000 | [diff] [blame] | 262 | if (getLangOpts().MSVCCompat) { |
| 263 | if (getLangOpts().CPlusPlus && |
David Majnemer | be52539 | 2015-02-18 02:28:13 +0000 | [diff] [blame] | 264 | IdResolver.begin(&Context.Idents.get("type_info")) == IdResolver.end()) |
Alp Toker | e1fab52 | 2014-01-04 15:25:02 +0000 | [diff] [blame] | 265 | PushOnScopeChains(Context.buildImplicitRecord("type_info", TTK_Class), |
| 266 | TUScope); |
David Majnemer | 1de3691 | 2014-01-14 06:19:35 +0000 | [diff] [blame] | 267 | |
| 268 | addImplicitTypedef("size_t", Context.getSizeType()); |
Alp Toker | e1fab52 | 2014-01-04 15:25:02 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 271 | // Initialize predefined OpenCL types and supported extensions and (optional) |
| 272 | // core features. |
Craig Topper | 1212626 | 2015-11-15 17:27:57 +0000 | [diff] [blame] | 273 | if (getLangOpts().OpenCL) { |
Anastasia Stulova | e88e2b9 | 2019-02-07 17:32:37 +0000 | [diff] [blame] | 274 | getOpenCLOptions().addSupport( |
| 275 | Context.getTargetInfo().getSupportedOpenCLOpts()); |
| 276 | getOpenCLOptions().enableSupportedCore(getLangOpts()); |
Alp Toker | f22856a | 2013-12-18 15:29:05 +0000 | [diff] [blame] | 277 | addImplicitTypedef("sampler_t", Context.OCLSamplerTy); |
| 278 | addImplicitTypedef("event_t", Context.OCLEventTy); |
Anastasia Stulova | e88e2b9 | 2019-02-07 17:32:37 +0000 | [diff] [blame] | 279 | if (getLangOpts().OpenCLCPlusPlus || getLangOpts().OpenCLVersion >= 200) { |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 280 | addImplicitTypedef("clk_event_t", Context.OCLClkEventTy); |
| 281 | addImplicitTypedef("queue_t", Context.OCLQueueTy); |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 282 | addImplicitTypedef("reserve_id_t", Context.OCLReserveIDTy); |
Anastasia Stulova | b1152f1 | 2015-03-18 12:55:29 +0000 | [diff] [blame] | 283 | addImplicitTypedef("atomic_int", Context.getAtomicType(Context.IntTy)); |
| 284 | addImplicitTypedef("atomic_uint", |
| 285 | Context.getAtomicType(Context.UnsignedIntTy)); |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 286 | auto AtomicLongT = Context.getAtomicType(Context.LongTy); |
| 287 | addImplicitTypedef("atomic_long", AtomicLongT); |
| 288 | auto AtomicULongT = Context.getAtomicType(Context.UnsignedLongTy); |
| 289 | addImplicitTypedef("atomic_ulong", AtomicULongT); |
Anastasia Stulova | b1152f1 | 2015-03-18 12:55:29 +0000 | [diff] [blame] | 290 | addImplicitTypedef("atomic_float", |
| 291 | Context.getAtomicType(Context.FloatTy)); |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 292 | auto AtomicDoubleT = Context.getAtomicType(Context.DoubleTy); |
| 293 | addImplicitTypedef("atomic_double", AtomicDoubleT); |
Anastasia Stulova | b1152f1 | 2015-03-18 12:55:29 +0000 | [diff] [blame] | 294 | // OpenCLC v2.0, s6.13.11.6 requires that atomic_flag is implemented as |
| 295 | // 32-bit integer and OpenCLC v2.0, s6.1.1 int is always 32-bit wide. |
| 296 | addImplicitTypedef("atomic_flag", Context.getAtomicType(Context.IntTy)); |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 297 | auto AtomicIntPtrT = Context.getAtomicType(Context.getIntPtrType()); |
| 298 | addImplicitTypedef("atomic_intptr_t", AtomicIntPtrT); |
| 299 | auto AtomicUIntPtrT = Context.getAtomicType(Context.getUIntPtrType()); |
| 300 | addImplicitTypedef("atomic_uintptr_t", AtomicUIntPtrT); |
| 301 | auto AtomicSizeT = Context.getAtomicType(Context.getSizeType()); |
| 302 | addImplicitTypedef("atomic_size_t", AtomicSizeT); |
| 303 | auto AtomicPtrDiffT = Context.getAtomicType(Context.getPointerDiffType()); |
| 304 | addImplicitTypedef("atomic_ptrdiff_t", AtomicPtrDiffT); |
| 305 | |
| 306 | // OpenCL v2.0 s6.13.11.6: |
| 307 | // - The atomic_long and atomic_ulong types are supported if the |
| 308 | // cl_khr_int64_base_atomics and cl_khr_int64_extended_atomics |
| 309 | // extensions are supported. |
| 310 | // - The atomic_double type is only supported if double precision |
| 311 | // is supported and the cl_khr_int64_base_atomics and |
| 312 | // cl_khr_int64_extended_atomics extensions are supported. |
| 313 | // - If the device address space is 64-bits, the data types |
| 314 | // atomic_intptr_t, atomic_uintptr_t, atomic_size_t and |
| 315 | // atomic_ptrdiff_t are supported if the cl_khr_int64_base_atomics and |
| 316 | // cl_khr_int64_extended_atomics extensions are supported. |
| 317 | std::vector<QualType> Atomic64BitTypes; |
| 318 | Atomic64BitTypes.push_back(AtomicLongT); |
| 319 | Atomic64BitTypes.push_back(AtomicULongT); |
| 320 | Atomic64BitTypes.push_back(AtomicDoubleT); |
| 321 | if (Context.getTypeSize(AtomicSizeT) == 64) { |
| 322 | Atomic64BitTypes.push_back(AtomicSizeT); |
| 323 | Atomic64BitTypes.push_back(AtomicIntPtrT); |
| 324 | Atomic64BitTypes.push_back(AtomicUIntPtrT); |
| 325 | Atomic64BitTypes.push_back(AtomicPtrDiffT); |
| 326 | } |
| 327 | for (auto &I : Atomic64BitTypes) |
| 328 | setOpenCLExtensionForType(I, |
| 329 | "cl_khr_int64_base_atomics cl_khr_int64_extended_atomics"); |
| 330 | |
| 331 | setOpenCLExtensionForType(AtomicDoubleT, "cl_khr_fp64"); |
Anastasia Stulova | b1152f1 | 2015-03-18 12:55:29 +0000 | [diff] [blame] | 332 | } |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 333 | |
| 334 | setOpenCLExtensionForType(Context.DoubleTy, "cl_khr_fp64"); |
| 335 | |
| 336 | #define GENERIC_IMAGE_TYPE_EXT(Type, Id, Ext) \ |
| 337 | setOpenCLExtensionForType(Context.Id, Ext); |
| 338 | #include "clang/Basic/OpenCLImageTypes.def" |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 339 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
| 340 | addImplicitTypedef(#ExtType, Context.Id##Ty); \ |
| 341 | setOpenCLExtensionForType(Context.Id##Ty, #Ext); |
| 342 | #include "clang/Basic/OpenCLExtensionTypes.def" |
Richard Sandiford | eb485fb | 2019-08-09 08:52:54 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | if (Context.getTargetInfo().hasAArch64SVETypes()) { |
| 346 | #define SVE_TYPE(Name, Id, SingletonId) \ |
| 347 | addImplicitTypedef(Name, Context.SingletonId); |
| 348 | #include "clang/Basic/AArch64SVEACLETypes.def" |
| 349 | } |
Alp Toker | f22856a | 2013-12-18 15:29:05 +0000 | [diff] [blame] | 350 | |
Craig Topper | 1212626 | 2015-11-15 17:27:57 +0000 | [diff] [blame] | 351 | if (Context.getTargetInfo().hasBuiltinMSVaList()) { |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 352 | DeclarationName MSVaList = &Context.Idents.get("__builtin_ms_va_list"); |
| 353 | if (IdResolver.begin(MSVaList) == IdResolver.end()) |
| 354 | PushOnScopeChains(Context.getBuiltinMSVaListDecl(), TUScope); |
| 355 | } |
| 356 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 357 | DeclarationName BuiltinVaList = &Context.Idents.get("__builtin_va_list"); |
| 358 | if (IdResolver.begin(BuiltinVaList) == IdResolver.end()) |
| 359 | PushOnScopeChains(Context.getBuiltinVaListDecl(), TUScope); |
Steve Naroff | 38d31b4 | 2007-02-28 01:22:02 +0000 | [diff] [blame] | 360 | } |
Chris Lattner | cb6a382 | 2006-11-10 06:20:45 +0000 | [diff] [blame] | 361 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 362 | Sema::~Sema() { |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 363 | if (VisContext) FreeVisContext(); |
Reid Kleckner | 87a3180 | 2018-03-12 21:43:02 +0000 | [diff] [blame] | 364 | |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 365 | // Kill all the active scopes. |
Reid Kleckner | 87a3180 | 2018-03-12 21:43:02 +0000 | [diff] [blame] | 366 | for (sema::FunctionScopeInfo *FSI : FunctionScopes) |
Richard Smith | 2fdd95c | 2019-05-31 00:45:09 +0000 | [diff] [blame] | 367 | delete FSI; |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 368 | |
Douglas Gregor | 5c6f10b | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 369 | // Tell the SemaConsumer to forget about us; we're going out of scope. |
| 370 | if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer)) |
| 371 | SC->ForgetSema(); |
| 372 | |
| 373 | // Detach from the external Sema source. |
| 374 | if (ExternalSemaSource *ExternalSema |
Douglas Gregor | 6fd55e0 | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 375 | = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource())) |
Douglas Gregor | 5c6f10b | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 376 | ExternalSema->ForgetSema(); |
Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 377 | |
| 378 | // If Sema's ExternalSource is the multiplexer - we own it. |
| 379 | if (isMultiplexExternalSource) |
| 380 | delete ExternalSource; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 381 | |
DeLesley Hutchins | ab1dc2d | 2015-02-03 22:11:04 +0000 | [diff] [blame] | 382 | threadSafety::threadSafetyCleanup(ThreadSafetyDeclCache); |
| 383 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 384 | // Destroys data sharing attributes stack for OpenMP |
| 385 | DestroyDataSharingAttributesStack(); |
Kaelyn Takata | ef3e42b | 2014-11-21 18:48:06 +0000 | [diff] [blame] | 386 | |
Alex Lorenz | 45b4014 | 2017-07-28 14:41:21 +0000 | [diff] [blame] | 387 | // Detach from the PP callback handler which outlives Sema since it's owned |
| 388 | // by the preprocessor. |
| 389 | SemaPPCallbackHandler->reset(); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Richard Smith | 26a92d5 | 2019-08-26 18:18:07 +0000 | [diff] [blame] | 392 | void Sema::warnStackExhausted(SourceLocation Loc) { |
| 393 | // Only warn about this once. |
| 394 | if (!WarnedStackExhausted) { |
| 395 | Diag(Loc, diag::warn_stack_exhausted); |
| 396 | WarnedStackExhausted = true; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | void Sema::runWithSufficientStackSpace(SourceLocation Loc, |
| 401 | llvm::function_ref<void()> Fn) { |
| 402 | clang::runWithSufficientStackSpace([&] { warnStackExhausted(Loc); }, Fn); |
| 403 | } |
| 404 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 405 | /// makeUnavailableInSystemHeader - There is an error in the current |
| 406 | /// context. If we're still in a system header, and we can plausibly |
| 407 | /// make the relevant declaration unavailable instead of erroring, do |
| 408 | /// so and return true. |
| 409 | bool Sema::makeUnavailableInSystemHeader(SourceLocation loc, |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 410 | UnavailableAttr::ImplicitReason reason) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 411 | // If we're not in a function, it's an error. |
| 412 | FunctionDecl *fn = dyn_cast<FunctionDecl>(CurContext); |
| 413 | if (!fn) return false; |
| 414 | |
| 415 | // If we're in template instantiation, it's an error. |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 416 | if (inTemplateInstantiation()) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 417 | return false; |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 418 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 419 | // If that function's not in a system header, it's an error. |
| 420 | if (!Context.getSourceManager().isInSystemHeader(loc)) |
| 421 | return false; |
| 422 | |
| 423 | // If the function is already unavailable, it's not an error. |
| 424 | if (fn->hasAttr<UnavailableAttr>()) return true; |
| 425 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 426 | fn->addAttr(UnavailableAttr::CreateImplicit(Context, "", reason, loc)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 427 | return true; |
| 428 | } |
| 429 | |
Sebastian Redl | ab238a7 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 430 | ASTMutationListener *Sema::getASTMutationListener() const { |
| 431 | return getASTConsumer().GetASTMutationListener(); |
| 432 | } |
| 433 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 434 | ///Registers an external source. If an external source already exists, |
Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 435 | /// creates a multiplex external source and appends to it. |
| 436 | /// |
| 437 | ///\param[in] E - A non-null external sema source. |
| 438 | /// |
| 439 | void Sema::addExternalSource(ExternalSemaSource *E) { |
| 440 | assert(E && "Cannot use with NULL ptr"); |
| 441 | |
| 442 | if (!ExternalSource) { |
| 443 | ExternalSource = E; |
| 444 | return; |
| 445 | } |
| 446 | |
| 447 | if (isMultiplexExternalSource) |
| 448 | static_cast<MultiplexExternalSemaSource*>(ExternalSource)->addSource(*E); |
| 449 | else { |
| 450 | ExternalSource = new MultiplexExternalSemaSource(*ExternalSource, *E); |
| 451 | isMultiplexExternalSource = true; |
| 452 | } |
| 453 | } |
| 454 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 455 | /// Print out statistics about the semantic analysis. |
Chandler Carruth | b4836ea | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 456 | void Sema::PrintStats() const { |
| 457 | llvm::errs() << "\n*** Semantic Analysis Stats:\n"; |
| 458 | llvm::errs() << NumSFINAEErrors << " SFINAE diagnostics trapped.\n"; |
| 459 | |
| 460 | BumpAlloc.PrintStats(); |
| 461 | AnalysisWarnings.PrintStats(); |
| 462 | } |
| 463 | |
George Burgess IV | 8d141e0 | 2015-12-14 22:00:49 +0000 | [diff] [blame] | 464 | void Sema::diagnoseNullableToNonnullConversion(QualType DstType, |
| 465 | QualType SrcType, |
| 466 | SourceLocation Loc) { |
| 467 | Optional<NullabilityKind> ExprNullability = SrcType->getNullability(Context); |
| 468 | if (!ExprNullability || *ExprNullability != NullabilityKind::Nullable) |
| 469 | return; |
| 470 | |
| 471 | Optional<NullabilityKind> TypeNullability = DstType->getNullability(Context); |
| 472 | if (!TypeNullability || *TypeNullability != NullabilityKind::NonNull) |
| 473 | return; |
| 474 | |
| 475 | Diag(Loc, diag::warn_nullability_lost) << SrcType << DstType; |
| 476 | } |
| 477 | |
Nico Weber | d7ba86b | 2017-05-05 16:11:08 +0000 | [diff] [blame] | 478 | void Sema::diagnoseZeroToNullptrConversion(CastKind Kind, const Expr* E) { |
Roman Lebedev | 809df34 | 2017-10-26 13:18:14 +0000 | [diff] [blame] | 479 | if (Diags.isIgnored(diag::warn_zero_as_null_pointer_constant, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 480 | E->getBeginLoc())) |
Roman Lebedev | 809df34 | 2017-10-26 13:18:14 +0000 | [diff] [blame] | 481 | return; |
| 482 | // nullptr only exists from C++11 on, so don't warn on its absence earlier. |
| 483 | if (!getLangOpts().CPlusPlus11) |
| 484 | return; |
| 485 | |
Nico Weber | d7ba86b | 2017-05-05 16:11:08 +0000 | [diff] [blame] | 486 | if (Kind != CK_NullToPointer && Kind != CK_NullToMemberPointer) |
| 487 | return; |
Erich Keane | 818cf5b | 2017-10-25 20:23:13 +0000 | [diff] [blame] | 488 | if (E->IgnoreParenImpCasts()->getType()->isNullPtrType()) |
Nico Weber | d7ba86b | 2017-05-05 16:11:08 +0000 | [diff] [blame] | 489 | return; |
Roman Lebedev | 809df34 | 2017-10-26 13:18:14 +0000 | [diff] [blame] | 490 | |
| 491 | // If it is a macro from system header, and if the macro name is not "NULL", |
| 492 | // do not warn. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 493 | SourceLocation MaybeMacroLoc = E->getBeginLoc(); |
Roman Lebedev | 809df34 | 2017-10-26 13:18:14 +0000 | [diff] [blame] | 494 | if (Diags.getSuppressSystemWarnings() && |
| 495 | SourceMgr.isInSystemMacro(MaybeMacroLoc) && |
| 496 | !findMacroSpelling(MaybeMacroLoc, "NULL")) |
Nico Weber | d7ba86b | 2017-05-05 16:11:08 +0000 | [diff] [blame] | 497 | return; |
| 498 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 499 | Diag(E->getBeginLoc(), diag::warn_zero_as_null_pointer_constant) |
Nico Weber | d7ba86b | 2017-05-05 16:11:08 +0000 | [diff] [blame] | 500 | << FixItHint::CreateReplacement(E->getSourceRange(), "nullptr"); |
| 501 | } |
| 502 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 503 | /// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. |
| 504 | /// If there is already an implicit cast, merge into the existing one. |
| 505 | /// The result is of the given category. |
| 506 | ExprResult Sema::ImpCastExprToType(Expr *E, QualType Ty, |
| 507 | CastKind Kind, ExprValueKind VK, |
| 508 | const CXXCastPath *BasePath, |
| 509 | CheckedConversionKind CCK) { |
Richard Smith | 508ebf3 | 2011-10-28 03:31:48 +0000 | [diff] [blame] | 510 | #ifndef NDEBUG |
| 511 | if (VK == VK_RValue && !E->isRValue()) { |
| 512 | switch (Kind) { |
| 513 | default: |
Craig Topper | d8d4319 | 2014-06-18 05:13:13 +0000 | [diff] [blame] | 514 | llvm_unreachable("can't implicitly cast lvalue to rvalue with this cast " |
| 515 | "kind"); |
Richard Smith | 07ed9cf | 2019-06-20 19:49:13 +0000 | [diff] [blame] | 516 | case CK_Dependent: |
Richard Smith | 508ebf3 | 2011-10-28 03:31:48 +0000 | [diff] [blame] | 517 | case CK_LValueToRValue: |
| 518 | case CK_ArrayToPointerDecay: |
| 519 | case CK_FunctionToPointerDecay: |
| 520 | case CK_ToVoid: |
JF Bastien | 7d60a0f | 2018-07-18 18:01:41 +0000 | [diff] [blame] | 521 | case CK_NonAtomicToAtomic: |
Richard Smith | 508ebf3 | 2011-10-28 03:31:48 +0000 | [diff] [blame] | 522 | break; |
| 523 | } |
| 524 | } |
Richard Smith | 07ed9cf | 2019-06-20 19:49:13 +0000 | [diff] [blame] | 525 | assert((VK == VK_RValue || Kind == CK_Dependent || !E->isRValue()) && |
| 526 | "can't cast rvalue to lvalue"); |
Richard Smith | 508ebf3 | 2011-10-28 03:31:48 +0000 | [diff] [blame] | 527 | #endif |
| 528 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 529 | diagnoseNullableToNonnullConversion(Ty, E->getType(), E->getBeginLoc()); |
Nico Weber | d7ba86b | 2017-05-05 16:11:08 +0000 | [diff] [blame] | 530 | diagnoseZeroToNullptrConversion(Kind, E); |
Douglas Gregor | b4866e8 | 2015-06-19 18:13:19 +0000 | [diff] [blame] | 531 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 532 | QualType ExprTy = Context.getCanonicalType(E->getType()); |
Mon P Wang | 74b3207 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 533 | QualType TypeTy = Context.getCanonicalType(Ty); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 534 | |
Mon P Wang | 74b3207 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 535 | if (ExprTy == TypeTy) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 536 | return E; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 537 | |
Richard Smith | b3189a1 | 2016-12-05 07:49:14 +0000 | [diff] [blame] | 538 | // C++1z [conv.array]: The temporary materialization conversion is applied. |
| 539 | // We also use this to fuel C++ DR1213, which applies to C++11 onwards. |
| 540 | if (Kind == CK_ArrayToPointerDecay && getLangOpts().CPlusPlus && |
| 541 | E->getValueKind() == VK_RValue) { |
| 542 | // The temporary is an lvalue in C++98 and an xvalue otherwise. |
| 543 | ExprResult Materialized = CreateMaterializeTemporaryExpr( |
| 544 | E->getType(), E, !getLangOpts().CPlusPlus11); |
| 545 | if (Materialized.isInvalid()) |
| 546 | return ExprError(); |
| 547 | E = Materialized.get(); |
| 548 | } |
| 549 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 550 | if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(E)) { |
| 551 | if (ImpCast->getCastKind() == Kind && (!BasePath || BasePath->empty())) { |
| 552 | ImpCast->setType(Ty); |
| 553 | ImpCast->setValueKind(VK); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 554 | return E; |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 558 | return ImplicitCastExpr::Create(Context, Ty, Kind, E, BasePath, VK); |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Abramo Bagnara | 7ccce98 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 561 | /// ScalarTypeToBooleanCastKind - Returns the cast kind corresponding |
| 562 | /// to the conversion from scalar type ScalarTy to the Boolean type. |
| 563 | CastKind Sema::ScalarTypeToBooleanCastKind(QualType ScalarTy) { |
| 564 | switch (ScalarTy->getScalarTypeKind()) { |
| 565 | case Type::STK_Bool: return CK_NoOp; |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 566 | case Type::STK_CPointer: return CK_PointerToBoolean; |
| 567 | case Type::STK_BlockPointer: return CK_PointerToBoolean; |
| 568 | case Type::STK_ObjCObjectPointer: return CK_PointerToBoolean; |
Abramo Bagnara | 7ccce98 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 569 | case Type::STK_MemberPointer: return CK_MemberPointerToBoolean; |
| 570 | case Type::STK_Integral: return CK_IntegralToBoolean; |
| 571 | case Type::STK_Floating: return CK_FloatingToBoolean; |
| 572 | case Type::STK_IntegralComplex: return CK_IntegralComplexToBoolean; |
| 573 | case Type::STK_FloatingComplex: return CK_FloatingComplexToBoolean; |
Leonard Chan | b4ba467 | 2018-10-23 17:55:35 +0000 | [diff] [blame] | 574 | case Type::STK_FixedPoint: return CK_FixedPointToBoolean; |
Abramo Bagnara | 7ccce98 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 575 | } |
Richard Smith | 354abec | 2017-12-08 23:29:59 +0000 | [diff] [blame] | 576 | llvm_unreachable("unknown scalar type kind"); |
Abramo Bagnara | 7ccce98 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 579 | /// Used to prune the decls of Sema's UnusedFileScopedDecls vector. |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 580 | static bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) { |
Rafael Espindola | 820fa70 | 2013-01-08 19:43:34 +0000 | [diff] [blame] | 581 | if (D->getMostRecentDecl()->isUsed()) |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 582 | return true; |
| 583 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 584 | if (D->isExternallyVisible()) |
Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 585 | return true; |
| 586 | |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 587 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Vassil Vassilev | 64e1e1e | 2017-05-09 11:25:41 +0000 | [diff] [blame] | 588 | // If this is a function template and none of its specializations is used, |
| 589 | // we should warn. |
| 590 | if (FunctionTemplateDecl *Template = FD->getDescribedFunctionTemplate()) |
| 591 | for (const auto *Spec : Template->specializations()) |
| 592 | if (ShouldRemoveFromUnused(SemaRef, Spec)) |
| 593 | return true; |
| 594 | |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 595 | // UnusedFileScopedDecls stores the first declaration. |
| 596 | // The declaration may have become definition so check again. |
| 597 | const FunctionDecl *DeclToCheck; |
| 598 | if (FD->hasBody(DeclToCheck)) |
| 599 | return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); |
| 600 | |
| 601 | // Later redecls may add new information resulting in not having to warn, |
| 602 | // so check again. |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 603 | DeclToCheck = FD->getMostRecentDecl(); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 604 | if (DeclToCheck != FD) |
| 605 | return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); |
| 606 | } |
| 607 | |
| 608 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Eli Friedman | a5dfebd | 2013-09-10 21:10:25 +0000 | [diff] [blame] | 609 | // If a variable usable in constant expressions is referenced, |
| 610 | // don't warn if it isn't used: if the value of a variable is required |
| 611 | // for the computation of a constant expression, it doesn't make sense to |
| 612 | // warn even if the variable isn't odr-used. (isReferenced doesn't |
| 613 | // precisely reflect that, but it's a decent approximation.) |
| 614 | if (VD->isReferenced() && |
Richard Smith | 715f7a1 | 2019-06-11 17:50:32 +0000 | [diff] [blame] | 615 | VD->mightBeUsableInConstantExpressions(SemaRef->Context)) |
Eli Friedman | a5dfebd | 2013-09-10 21:10:25 +0000 | [diff] [blame] | 616 | return true; |
| 617 | |
Vassil Vassilev | 64e1e1e | 2017-05-09 11:25:41 +0000 | [diff] [blame] | 618 | if (VarTemplateDecl *Template = VD->getDescribedVarTemplate()) |
| 619 | // If this is a variable template and none of its specializations is used, |
| 620 | // we should warn. |
| 621 | for (const auto *Spec : Template->specializations()) |
| 622 | if (ShouldRemoveFromUnused(SemaRef, Spec)) |
| 623 | return true; |
| 624 | |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 625 | // UnusedFileScopedDecls stores the first declaration. |
| 626 | // The declaration may have become definition so check again. |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 627 | const VarDecl *DeclToCheck = VD->getDefinition(); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 628 | if (DeclToCheck) |
| 629 | return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); |
| 630 | |
| 631 | // Later redecls may add new information resulting in not having to warn, |
| 632 | // so check again. |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 633 | DeclToCheck = VD->getMostRecentDecl(); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 634 | if (DeclToCheck != VD) |
| 635 | return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); |
| 636 | } |
| 637 | |
| 638 | return false; |
| 639 | } |
| 640 | |
Richard Smith | 405e2db | 2017-09-20 07:22:00 +0000 | [diff] [blame] | 641 | static bool isFunctionOrVarDeclExternC(NamedDecl *ND) { |
| 642 | if (auto *FD = dyn_cast<FunctionDecl>(ND)) |
| 643 | return FD->isExternC(); |
| 644 | return cast<VarDecl>(ND)->isExternC(); |
| 645 | } |
| 646 | |
| 647 | /// Determine whether ND is an external-linkage function or variable whose |
| 648 | /// type has no linkage. |
| 649 | bool Sema::isExternalWithNoLinkageType(ValueDecl *VD) { |
| 650 | // Note: it's not quite enough to check whether VD has UniqueExternalLinkage, |
| 651 | // because we also want to catch the case where its type has VisibleNoLinkage, |
| 652 | // which does not affect the linkage of VD. |
| 653 | return getLangOpts().CPlusPlus && VD->hasExternalFormalLinkage() && |
| 654 | !isExternalFormalLinkage(VD->getType()->getLinkage()) && |
| 655 | !isFunctionOrVarDeclExternC(VD); |
| 656 | } |
| 657 | |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 658 | /// Obtains a sorted list of functions and variables that are undefined but |
| 659 | /// ODR-used. |
Nick Lewycky | 9c7eb1d | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 660 | void Sema::getUndefinedButUsed( |
Nick Lewycky | f0f5616 | 2013-01-31 03:23:57 +0000 | [diff] [blame] | 661 | SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> > &Undefined) { |
Richard Smith | d6a04d7 | 2016-03-25 21:49:43 +0000 | [diff] [blame] | 662 | for (const auto &UndefinedUse : UndefinedButUsed) { |
| 663 | NamedDecl *ND = UndefinedUse.first; |
Nick Lewycky | f0f5616 | 2013-01-31 03:23:57 +0000 | [diff] [blame] | 664 | |
| 665 | // Ignore attributes that have become invalid. |
| 666 | if (ND->isInvalidDecl()) continue; |
| 667 | |
Nick Lewycky | f0f5616 | 2013-01-31 03:23:57 +0000 | [diff] [blame] | 668 | // __attribute__((weakref)) is basically a definition. |
| 669 | if (ND->hasAttr<WeakRefAttr>()) continue; |
| 670 | |
Richard Smith | 5786582 | 2017-08-03 19:24:27 +0000 | [diff] [blame] | 671 | if (isa<CXXDeductionGuideDecl>(ND)) |
| 672 | continue; |
| 673 | |
Richard Smith | 405e2db | 2017-09-20 07:22:00 +0000 | [diff] [blame] | 674 | if (ND->hasAttr<DLLImportAttr>() || ND->hasAttr<DLLExportAttr>()) { |
| 675 | // An exported function will always be emitted when defined, so even if |
| 676 | // the function is inline, it doesn't have to be emitted in this TU. An |
| 677 | // imported function implies that it has been exported somewhere else. |
| 678 | continue; |
| 679 | } |
| 680 | |
Nick Lewycky | f0f5616 | 2013-01-31 03:23:57 +0000 | [diff] [blame] | 681 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 682 | if (FD->isDefined()) |
| 683 | continue; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 684 | if (FD->isExternallyVisible() && |
Richard Smith | 405e2db | 2017-09-20 07:22:00 +0000 | [diff] [blame] | 685 | !isExternalWithNoLinkageType(FD) && |
Louis Dionne | d269579 | 2018-10-04 15:49:42 +0000 | [diff] [blame] | 686 | !FD->getMostRecentDecl()->isInlined() && |
| 687 | !FD->hasAttr<ExcludeFromExplicitInstantiationAttr>()) |
Nick Lewycky | 9c7eb1d | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 688 | continue; |
Reid Kleckner | c8ae878 | 2018-06-20 21:12:20 +0000 | [diff] [blame] | 689 | if (FD->getBuiltinID()) |
| 690 | continue; |
Nick Lewycky | f0f5616 | 2013-01-31 03:23:57 +0000 | [diff] [blame] | 691 | } else { |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 692 | auto *VD = cast<VarDecl>(ND); |
| 693 | if (VD->hasDefinition() != VarDecl::DeclarationOnly) |
Nick Lewycky | f0f5616 | 2013-01-31 03:23:57 +0000 | [diff] [blame] | 694 | continue; |
Richard Smith | 405e2db | 2017-09-20 07:22:00 +0000 | [diff] [blame] | 695 | if (VD->isExternallyVisible() && |
| 696 | !isExternalWithNoLinkageType(VD) && |
Louis Dionne | d269579 | 2018-10-04 15:49:42 +0000 | [diff] [blame] | 697 | !VD->getMostRecentDecl()->isInline() && |
| 698 | !VD->hasAttr<ExcludeFromExplicitInstantiationAttr>()) |
Nick Lewycky | 9c7eb1d | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 699 | continue; |
Justin Lebar | 5489f85 | 2018-05-17 16:15:07 +0000 | [diff] [blame] | 700 | |
| 701 | // Skip VarDecls that lack formal definitions but which we know are in |
| 702 | // fact defined somewhere. |
| 703 | if (VD->isKnownToBeDefined()) |
| 704 | continue; |
Nick Lewycky | f0f5616 | 2013-01-31 03:23:57 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Richard Smith | d6a04d7 | 2016-03-25 21:49:43 +0000 | [diff] [blame] | 707 | Undefined.push_back(std::make_pair(ND, UndefinedUse.second)); |
Nick Lewycky | f0f5616 | 2013-01-31 03:23:57 +0000 | [diff] [blame] | 708 | } |
Nick Lewycky | f0f5616 | 2013-01-31 03:23:57 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Nick Lewycky | 9c7eb1d | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 711 | /// checkUndefinedButUsed - Check for undefined objects with internal linkage |
| 712 | /// or that are inline. |
| 713 | static void checkUndefinedButUsed(Sema &S) { |
| 714 | if (S.UndefinedButUsed.empty()) return; |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 715 | |
| 716 | // Collect all the still-undefined entities with internal linkage. |
Nick Lewycky | f0f5616 | 2013-01-31 03:23:57 +0000 | [diff] [blame] | 717 | SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined; |
Nick Lewycky | 9c7eb1d | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 718 | S.getUndefinedButUsed(Undefined); |
Nick Lewycky | f0f5616 | 2013-01-31 03:23:57 +0000 | [diff] [blame] | 719 | if (Undefined.empty()) return; |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 720 | |
Richard Smith | 405e2db | 2017-09-20 07:22:00 +0000 | [diff] [blame] | 721 | for (auto Undef : Undefined) { |
| 722 | ValueDecl *VD = cast<ValueDecl>(Undef.first); |
| 723 | SourceLocation UseLoc = Undef.second; |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 724 | |
Richard Smith | 405e2db | 2017-09-20 07:22:00 +0000 | [diff] [blame] | 725 | if (S.isExternalWithNoLinkageType(VD)) { |
| 726 | // C++ [basic.link]p8: |
| 727 | // A type without linkage shall not be used as the type of a variable |
| 728 | // or function with external linkage unless |
| 729 | // -- the entity has C language linkage |
| 730 | // -- the entity is not odr-used or is defined in the same TU |
| 731 | // |
| 732 | // As an extension, accept this in cases where the type is externally |
| 733 | // visible, since the function or variable actually can be defined in |
| 734 | // another translation unit in that case. |
| 735 | S.Diag(VD->getLocation(), isExternallyVisible(VD->getType()->getLinkage()) |
| 736 | ? diag::ext_undefined_internal_type |
| 737 | : diag::err_undefined_internal_type) |
| 738 | << isa<VarDecl>(VD) << VD; |
| 739 | } else if (!VD->isExternallyVisible()) { |
| 740 | // FIXME: We can promote this to an error. The function or variable can't |
| 741 | // be defined anywhere else, so the program must necessarily violate the |
| 742 | // one definition rule. |
| 743 | S.Diag(VD->getLocation(), diag::warn_undefined_internal) |
| 744 | << isa<VarDecl>(VD) << VD; |
| 745 | } else if (auto *FD = dyn_cast<FunctionDecl>(VD)) { |
James Y Knight | 5b218ff | 2016-06-25 16:40:53 +0000 | [diff] [blame] | 746 | (void)FD; |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 747 | assert(FD->getMostRecentDecl()->isInlined() && |
Nick Lewycky | 9c7eb1d | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 748 | "used object requires definition but isn't inline or internal?"); |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 749 | // FIXME: This is ill-formed; we should reject. |
Richard Smith | 405e2db | 2017-09-20 07:22:00 +0000 | [diff] [blame] | 750 | S.Diag(VD->getLocation(), diag::warn_undefined_inline) << VD; |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 751 | } else { |
Richard Smith | 405e2db | 2017-09-20 07:22:00 +0000 | [diff] [blame] | 752 | assert(cast<VarDecl>(VD)->getMostRecentDecl()->isInline() && |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 753 | "used var requires definition but isn't inline or internal?"); |
Richard Smith | 405e2db | 2017-09-20 07:22:00 +0000 | [diff] [blame] | 754 | S.Diag(VD->getLocation(), diag::err_undefined_inline_var) << VD; |
Nick Lewycky | 9c7eb1d | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 755 | } |
Richard Smith | 405e2db | 2017-09-20 07:22:00 +0000 | [diff] [blame] | 756 | if (UseLoc.isValid()) |
| 757 | S.Diag(UseLoc, diag::note_used_here); |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 758 | } |
Richard Smith | d6a04d7 | 2016-03-25 21:49:43 +0000 | [diff] [blame] | 759 | |
| 760 | S.UndefinedButUsed.clear(); |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Douglas Gregor | 1c4bfe5 | 2011-07-28 18:09:57 +0000 | [diff] [blame] | 763 | void Sema::LoadExternalWeakUndeclaredIdentifiers() { |
| 764 | if (!ExternalSource) |
| 765 | return; |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 766 | |
Douglas Gregor | 1c4bfe5 | 2011-07-28 18:09:57 +0000 | [diff] [blame] | 767 | SmallVector<std::pair<IdentifierInfo *, WeakInfo>, 4> WeakIDs; |
| 768 | ExternalSource->ReadWeakUndeclaredIdentifiers(WeakIDs); |
Chandler Carruth | f85d982 | 2015-03-26 08:32:49 +0000 | [diff] [blame] | 769 | for (auto &WeakID : WeakIDs) |
| 770 | WeakUndeclaredIdentifiers.insert(WeakID); |
Douglas Gregor | 1c4bfe5 | 2011-07-28 18:09:57 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Daniel Jasper | 0baec549 | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 773 | |
| 774 | typedef llvm::DenseMap<const CXXRecordDecl*, bool> RecordCompleteMap; |
| 775 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 776 | /// Returns true, if all methods and nested classes of the given |
Daniel Jasper | 0baec549 | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 777 | /// CXXRecordDecl are defined in this translation unit. |
| 778 | /// |
| 779 | /// Should only be called from ActOnEndOfTranslationUnit so that all |
| 780 | /// definitions are actually read. |
| 781 | static bool MethodsAndNestedClassesComplete(const CXXRecordDecl *RD, |
| 782 | RecordCompleteMap &MNCComplete) { |
| 783 | RecordCompleteMap::iterator Cache = MNCComplete.find(RD); |
| 784 | if (Cache != MNCComplete.end()) |
| 785 | return Cache->second; |
| 786 | if (!RD->isCompleteDefinition()) |
| 787 | return false; |
| 788 | bool Complete = true; |
| 789 | for (DeclContext::decl_iterator I = RD->decls_begin(), |
| 790 | E = RD->decls_end(); |
| 791 | I != E && Complete; ++I) { |
| 792 | if (const CXXMethodDecl *M = dyn_cast<CXXMethodDecl>(*I)) |
Richard Smith | a31174e | 2017-11-01 04:52:12 +0000 | [diff] [blame] | 793 | Complete = M->isDefined() || M->isDefaulted() || |
| 794 | (M->isPure() && !isa<CXXDestructorDecl>(M)); |
Daniel Jasper | e99c2bf | 2012-06-14 20:56:06 +0000 | [diff] [blame] | 795 | else if (const FunctionTemplateDecl *F = dyn_cast<FunctionTemplateDecl>(*I)) |
Nico Weber | 4bde6c2 | 2015-06-18 20:09:49 +0000 | [diff] [blame] | 796 | // If the template function is marked as late template parsed at this |
| 797 | // point, it has not been instantiated and therefore we have not |
| 798 | // performed semantic analysis on it yet, so we cannot know if the type |
| 799 | // can be considered complete. |
Ehsan Akhgari | 4b5ca9a | 2014-10-11 00:24:15 +0000 | [diff] [blame] | 800 | Complete = !F->getTemplatedDecl()->isLateTemplateParsed() && |
| 801 | F->getTemplatedDecl()->isDefined(); |
Daniel Jasper | 0baec549 | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 802 | else if (const CXXRecordDecl *R = dyn_cast<CXXRecordDecl>(*I)) { |
| 803 | if (R->isInjectedClassName()) |
| 804 | continue; |
| 805 | if (R->hasDefinition()) |
| 806 | Complete = MethodsAndNestedClassesComplete(R->getDefinition(), |
| 807 | MNCComplete); |
| 808 | else |
| 809 | Complete = false; |
| 810 | } |
| 811 | } |
| 812 | MNCComplete[RD] = Complete; |
| 813 | return Complete; |
| 814 | } |
| 815 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 816 | /// Returns true, if the given CXXRecordDecl is fully defined in this |
Daniel Jasper | 0baec549 | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 817 | /// translation unit, i.e. all methods are defined or pure virtual and all |
| 818 | /// friends, friend functions and nested classes are fully defined in this |
| 819 | /// translation unit. |
| 820 | /// |
| 821 | /// Should only be called from ActOnEndOfTranslationUnit so that all |
| 822 | /// definitions are actually read. |
| 823 | static bool IsRecordFullyDefined(const CXXRecordDecl *RD, |
| 824 | RecordCompleteMap &RecordsComplete, |
| 825 | RecordCompleteMap &MNCComplete) { |
| 826 | RecordCompleteMap::iterator Cache = RecordsComplete.find(RD); |
| 827 | if (Cache != RecordsComplete.end()) |
| 828 | return Cache->second; |
| 829 | bool Complete = MethodsAndNestedClassesComplete(RD, MNCComplete); |
| 830 | for (CXXRecordDecl::friend_iterator I = RD->friend_begin(), |
| 831 | E = RD->friend_end(); |
| 832 | I != E && Complete; ++I) { |
| 833 | // Check if friend classes and methods are complete. |
| 834 | if (TypeSourceInfo *TSI = (*I)->getFriendType()) { |
| 835 | // Friend classes are available as the TypeSourceInfo of the FriendDecl. |
| 836 | if (CXXRecordDecl *FriendD = TSI->getType()->getAsCXXRecordDecl()) |
| 837 | Complete = MethodsAndNestedClassesComplete(FriendD, MNCComplete); |
| 838 | else |
| 839 | Complete = false; |
| 840 | } else { |
| 841 | // Friend functions are available through the NamedDecl of FriendDecl. |
| 842 | if (const FunctionDecl *FD = |
| 843 | dyn_cast<FunctionDecl>((*I)->getFriendDecl())) |
| 844 | Complete = FD->isDefined(); |
| 845 | else |
| 846 | // This is a template friend, give up. |
| 847 | Complete = false; |
| 848 | } |
| 849 | } |
| 850 | RecordsComplete[RD] = Complete; |
| 851 | return Complete; |
| 852 | } |
| 853 | |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 854 | void Sema::emitAndClearUnusedLocalTypedefWarnings() { |
| 855 | if (ExternalSource) |
| 856 | ExternalSource->ReadUnusedLocalTypedefNameCandidates( |
| 857 | UnusedLocalTypedefNameCandidates); |
| 858 | for (const TypedefNameDecl *TD : UnusedLocalTypedefNameCandidates) { |
| 859 | if (TD->isReferenced()) |
| 860 | continue; |
| 861 | Diag(TD->getLocation(), diag::warn_unused_local_typedef) |
| 862 | << isa<TypeAliasDecl>(TD) << TD->getDeclName(); |
| 863 | } |
| 864 | UnusedLocalTypedefNameCandidates.clear(); |
| 865 | } |
| 866 | |
Richard Smith | e03a654 | 2017-07-05 01:42:07 +0000 | [diff] [blame] | 867 | /// This is called before the very first declaration in the translation unit |
| 868 | /// is parsed. Note that the ASTContext may have already injected some |
| 869 | /// declarations. |
| 870 | void Sema::ActOnStartOfTranslationUnit() { |
Richard Smith | ac9f7fb | 2018-09-15 01:59:39 +0000 | [diff] [blame] | 871 | if (getLangOpts().ModulesTS && |
| 872 | (getLangOpts().getCompilingModule() == LangOptions::CMK_ModuleInterface || |
| 873 | getLangOpts().getCompilingModule() == LangOptions::CMK_None)) { |
Richard Smith | d652bdd | 2019-04-14 08:06:59 +0000 | [diff] [blame] | 874 | // We start in an implied global module fragment. |
Richard Smith | dd8b533 | 2017-09-04 05:37:53 +0000 | [diff] [blame] | 875 | SourceLocation StartOfTU = |
| 876 | SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); |
Richard Smith | d652bdd | 2019-04-14 08:06:59 +0000 | [diff] [blame] | 877 | ActOnGlobalModuleFragmentDecl(StartOfTU); |
| 878 | ModuleScopes.back().ImplicitGlobalModuleFragment = true; |
Richard Smith | e03a654 | 2017-07-05 01:42:07 +0000 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
Richard Smith | a5bbbfe | 2019-04-18 21:12:54 +0000 | [diff] [blame] | 882 | void Sema::ActOnEndOfTranslationUnitFragment(TUFragmentKind Kind) { |
| 883 | // No explicit actions are required at the end of the global module fragment. |
| 884 | if (Kind == TUFragmentKind::Global) |
Douglas Gregor | c459b51 | 2012-08-17 22:17:36 +0000 | [diff] [blame] | 885 | return; |
| 886 | |
Reid Kleckner | 24bd88c | 2018-03-26 18:22:47 +0000 | [diff] [blame] | 887 | // Transfer late parsed template instantiations over to the pending template |
Richard Smith | a5bbbfe | 2019-04-18 21:12:54 +0000 | [diff] [blame] | 888 | // instantiation list. During normal compilation, the late template parser |
Reid Kleckner | 24bd88c | 2018-03-26 18:22:47 +0000 | [diff] [blame] | 889 | // will be installed and instantiating these templates will succeed. |
| 890 | // |
| 891 | // If we are building a TU prefix for serialization, it is also safe to |
| 892 | // transfer these over, even though they are not parsed. The end of the TU |
| 893 | // should be outside of any eager template instantiation scope, so when this |
| 894 | // AST is deserialized, these templates will not be parsed until the end of |
| 895 | // the combined TU. |
| 896 | PendingInstantiations.insert(PendingInstantiations.end(), |
| 897 | LateParsedInstantiations.begin(), |
| 898 | LateParsedInstantiations.end()); |
| 899 | LateParsedInstantiations.clear(); |
| 900 | |
Richard Smith | a5bbbfe | 2019-04-18 21:12:54 +0000 | [diff] [blame] | 901 | // If DefinedUsedVTables ends up marking any virtual member functions it |
| 902 | // might lead to more pending template instantiations, which we then need |
| 903 | // to instantiate. |
| 904 | DefineUsedVTables(); |
| 905 | |
| 906 | // C++: Perform implicit template instantiations. |
| 907 | // |
| 908 | // FIXME: When we perform these implicit instantiations, we do not |
| 909 | // carefully keep track of the point of instantiation (C++ [temp.point]). |
| 910 | // This means that name lookup that occurs within the template |
| 911 | // instantiation will always happen at the end of the translation unit, |
| 912 | // so it will find some names that are not required to be found. This is |
| 913 | // valid, but we could do better by diagnosing if an instantiation uses a |
| 914 | // name that was not visible at its first point of instantiation. |
| 915 | if (ExternalSource) { |
| 916 | // Load pending instantiations from the external source. |
| 917 | SmallVector<PendingImplicitInstantiation, 4> Pending; |
| 918 | ExternalSource->ReadPendingInstantiations(Pending); |
| 919 | for (auto PII : Pending) |
| 920 | if (auto Func = dyn_cast<FunctionDecl>(PII.first)) |
| 921 | Func->setInstantiationIsPending(true); |
| 922 | PendingInstantiations.insert(PendingInstantiations.begin(), |
| 923 | Pending.begin(), Pending.end()); |
| 924 | } |
| 925 | |
| 926 | { |
| 927 | llvm::TimeTraceScope TimeScope("PerformPendingInstantiations", |
| 928 | StringRef("")); |
| 929 | PerformPendingInstantiations(); |
| 930 | } |
| 931 | |
Alexey Bataev | 729e242 | 2019-08-23 16:11:14 +0000 | [diff] [blame] | 932 | // Finalize analysis of OpenMP-specific constructs. |
| 933 | if (LangOpts.OpenMP) |
| 934 | finalizeOpenMPDelayedAnalysis(); |
| 935 | |
Richard Smith | a5bbbfe | 2019-04-18 21:12:54 +0000 | [diff] [blame] | 936 | assert(LateParsedInstantiations.empty() && |
| 937 | "end of TU template instantiation should not create more " |
| 938 | "late-parsed templates"); |
Ilya Biryukov | aeae71c | 2019-10-09 10:00:05 +0000 | [diff] [blame] | 939 | |
| 940 | // Report diagnostics for uncorrected delayed typos. Ideally all of them |
| 941 | // should have been corrected by that time, but it is very hard to cover all |
| 942 | // cases in practice. |
| 943 | for (const auto &Typo : DelayedTypos) { |
| 944 | // We pass an empty TypoCorrection to indicate no correction was performed. |
| 945 | Typo.second.DiagHandler(TypoCorrection()); |
| 946 | } |
| 947 | DelayedTypos.clear(); |
Richard Smith | a5bbbfe | 2019-04-18 21:12:54 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | /// ActOnEndOfTranslationUnit - This is called at the very end of the |
| 951 | /// translation unit when EOF is reached and all but the top-level scope is |
| 952 | /// popped. |
| 953 | void Sema::ActOnEndOfTranslationUnit() { |
| 954 | assert(DelayedDiagnostics.getCurrentPool() == nullptr |
| 955 | && "reached end of translation unit with a pool attached?"); |
| 956 | |
| 957 | // If code completion is enabled, don't perform any end-of-translation-unit |
| 958 | // work. |
| 959 | if (PP.isCodeCompletionEnabled()) |
| 960 | return; |
| 961 | |
Richard Smith | 0e5d7b8 | 2013-07-25 23:08:39 +0000 | [diff] [blame] | 962 | // Complete translation units and modules define vtables and perform implicit |
| 963 | // instantiations. PCH files do not. |
| 964 | if (TUKind != TU_Prefix) { |
Argyrios Kyrtzidis | b6c6a58 | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 965 | DiagnoseUseOfUnimplementedSelectors(); |
| 966 | |
Richard Smith | a5bbbfe | 2019-04-18 21:12:54 +0000 | [diff] [blame] | 967 | ActOnEndOfTranslationUnitFragment( |
| 968 | !ModuleScopes.empty() && ModuleScopes.back().Module->Kind == |
| 969 | Module::PrivateModuleFragment |
| 970 | ? TUFragmentKind::Private |
| 971 | : TUFragmentKind::Normal); |
Reid Kleckner | 24bd88c | 2018-03-26 18:22:47 +0000 | [diff] [blame] | 972 | |
Reid Kleckner | 89bd8d6 | 2014-10-22 17:50:19 +0000 | [diff] [blame] | 973 | if (LateTemplateParserCleanup) |
| 974 | LateTemplateParserCleanup(OpaqueParser); |
| 975 | |
Alp Toker | ae3a944 | 2013-10-18 05:54:19 +0000 | [diff] [blame] | 976 | CheckDelayedMemberExceptionSpecs(); |
Richard Smith | a5bbbfe | 2019-04-18 21:12:54 +0000 | [diff] [blame] | 977 | } else { |
| 978 | // If we are building a TU prefix for serialization, it is safe to transfer |
| 979 | // these over, even though they are not parsed. The end of the TU should be |
| 980 | // outside of any eager template instantiation scope, so when this AST is |
| 981 | // deserialized, these templates will not be parsed until the end of the |
| 982 | // combined TU. |
| 983 | PendingInstantiations.insert(PendingInstantiations.end(), |
| 984 | LateParsedInstantiations.begin(), |
| 985 | LateParsedInstantiations.end()); |
| 986 | LateParsedInstantiations.clear(); |
Nick Lewycky | ef4f456 | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 987 | } |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 988 | |
Alex Lorenz | 45b4014 | 2017-07-28 14:41:21 +0000 | [diff] [blame] | 989 | DiagnoseUnterminatedPragmaPack(); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 990 | DiagnoseUnterminatedPragmaAttribute(); |
| 991 | |
Alp Toker | ae3a944 | 2013-10-18 05:54:19 +0000 | [diff] [blame] | 992 | // All delayed member exception specs should be checked or we end up accepting |
| 993 | // incompatible declarations. |
Richard Smith | 5159bbad | 2018-09-05 22:30:37 +0000 | [diff] [blame] | 994 | assert(DelayedOverridingExceptionSpecChecks.empty()); |
| 995 | assert(DelayedEquivalentExceptionSpecChecks.empty()); |
Alp Toker | ae3a944 | 2013-10-18 05:54:19 +0000 | [diff] [blame] | 996 | |
Hans Wennborg | 99000c2 | 2015-08-15 01:18:16 +0000 | [diff] [blame] | 997 | // All dllexport classes should have been processed already. |
| 998 | assert(DelayedDllExportClasses.empty()); |
Hans Wennborg | c5877e9 | 2019-08-01 08:01:09 +0000 | [diff] [blame] | 999 | assert(DelayedDllExportMemberFunctions.empty()); |
Hans Wennborg | 99000c2 | 2015-08-15 01:18:16 +0000 | [diff] [blame] | 1000 | |
Argyrios Kyrtzidis | 35672e7 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 1001 | // Remove file scoped decls that turned out to be used. |
Daniel Jasper | 9ba6f9b | 2013-04-30 06:43:16 +0000 | [diff] [blame] | 1002 | UnusedFileScopedDecls.erase( |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1003 | std::remove_if(UnusedFileScopedDecls.begin(nullptr, true), |
Daniel Jasper | 9ba6f9b | 2013-04-30 06:43:16 +0000 | [diff] [blame] | 1004 | UnusedFileScopedDecls.end(), |
Richard Smith | 2c27df7 | 2017-03-23 23:17:58 +0000 | [diff] [blame] | 1005 | [this](const DeclaratorDecl *DD) { |
| 1006 | return ShouldRemoveFromUnused(this, DD); |
| 1007 | }), |
Daniel Jasper | 9ba6f9b | 2013-04-30 06:43:16 +0000 | [diff] [blame] | 1008 | UnusedFileScopedDecls.end()); |
Douglas Gregor | fb8b27d | 2010-04-09 17:41:13 +0000 | [diff] [blame] | 1009 | |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 1010 | if (TUKind == TU_Prefix) { |
| 1011 | // Translation unit prefixes don't need any of the checking below. |
Vassil Vassilev | 45bf62f | 2016-10-17 10:15:25 +0000 | [diff] [blame] | 1012 | if (!PP.isIncrementalProcessingEnabled()) |
| 1013 | TUScope = nullptr; |
Argyrios Kyrtzidis | ee1afa3 | 2010-08-05 09:48:08 +0000 | [diff] [blame] | 1014 | return; |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1015 | } |
Argyrios Kyrtzidis | ee1afa3 | 2010-08-05 09:48:08 +0000 | [diff] [blame] | 1016 | |
Chris Lattner | 0c79736 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 1017 | // Check for #pragma weak identifiers that were never declared |
Douglas Gregor | 1c4bfe5 | 2011-07-28 18:09:57 +0000 | [diff] [blame] | 1018 | LoadExternalWeakUndeclaredIdentifiers(); |
Chandler Carruth | f85d982 | 2015-03-26 08:32:49 +0000 | [diff] [blame] | 1019 | for (auto WeakID : WeakUndeclaredIdentifiers) { |
| 1020 | if (WeakID.second.getUsed()) |
| 1021 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1022 | |
Alexander Musman | fbbc0b8 | 2015-09-18 07:40:22 +0000 | [diff] [blame] | 1023 | Decl *PrevDecl = LookupSingleName(TUScope, WeakID.first, SourceLocation(), |
| 1024 | LookupOrdinaryName); |
| 1025 | if (PrevDecl != nullptr && |
| 1026 | !(isa<FunctionDecl>(PrevDecl) || isa<VarDecl>(PrevDecl))) |
| 1027 | Diag(WeakID.second.getLocation(), diag::warn_attribute_wrong_decl_type) |
| 1028 | << "'weak'" << ExpectedVariableOrFunction; |
| 1029 | else |
| 1030 | Diag(WeakID.second.getLocation(), diag::warn_weak_identifier_undeclared) |
| 1031 | << WeakID.first; |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Argyrios Kyrtzidis | ffb3558 | 2013-03-14 04:44:56 +0000 | [diff] [blame] | 1034 | if (LangOpts.CPlusPlus11 && |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 1035 | !Diags.isIgnored(diag::warn_delegating_ctor_cycle, SourceLocation())) |
Argyrios Kyrtzidis | ffb3558 | 2013-03-14 04:44:56 +0000 | [diff] [blame] | 1036 | CheckDelegatingCtorCycles(); |
| 1037 | |
Richard Smith | d6a04d7 | 2016-03-25 21:49:43 +0000 | [diff] [blame] | 1038 | if (!Diags.hasErrorOccurred()) { |
| 1039 | if (ExternalSource) |
| 1040 | ExternalSource->ReadUndefinedButUsed(UndefinedButUsed); |
| 1041 | checkUndefinedButUsed(*this); |
| 1042 | } |
| 1043 | |
Richard Smith | d652bdd | 2019-04-14 08:06:59 +0000 | [diff] [blame] | 1044 | // A global-module-fragment is only permitted within a module unit. |
| 1045 | bool DiagnosedMissingModuleDeclaration = false; |
| 1046 | if (!ModuleScopes.empty() && |
| 1047 | ModuleScopes.back().Module->Kind == Module::GlobalModuleFragment && |
| 1048 | !ModuleScopes.back().ImplicitGlobalModuleFragment) { |
| 1049 | Diag(ModuleScopes.back().BeginLoc, |
| 1050 | diag::err_module_declaration_missing_after_global_module_introducer); |
| 1051 | DiagnosedMissingModuleDeclaration = true; |
| 1052 | } |
| 1053 | |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 1054 | if (TUKind == TU_Module) { |
Richard Smith | 18057cb | 2017-10-11 00:36:56 +0000 | [diff] [blame] | 1055 | // If we are building a module interface unit, we need to have seen the |
| 1056 | // module declaration by now. |
| 1057 | if (getLangOpts().getCompilingModule() == |
| 1058 | LangOptions::CMK_ModuleInterface && |
Richard Smith | d6509cf | 2018-09-15 01:21:15 +0000 | [diff] [blame] | 1059 | (ModuleScopes.empty() || |
Richard Smith | a5bbbfe | 2019-04-18 21:12:54 +0000 | [diff] [blame] | 1060 | !ModuleScopes.back().Module->isModulePurview()) && |
Richard Smith | d652bdd | 2019-04-14 08:06:59 +0000 | [diff] [blame] | 1061 | !DiagnosedMissingModuleDeclaration) { |
Richard Smith | 18057cb | 2017-10-11 00:36:56 +0000 | [diff] [blame] | 1062 | // FIXME: Make a better guess as to where to put the module declaration. |
| 1063 | Diag(getSourceManager().getLocForStartOfFile( |
| 1064 | getSourceManager().getMainFileID()), |
| 1065 | diag::err_module_declaration_missing); |
| 1066 | } |
| 1067 | |
Douglas Gregor | 2b82c2a | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 1068 | // If we are building a module, resolve all of the exported declarations |
| 1069 | // now. |
| 1070 | if (Module *CurrentModule = PP.getCurrentModule()) { |
| 1071 | ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 1072 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1073 | SmallVector<Module *, 2> Stack; |
Douglas Gregor | 2b82c2a | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 1074 | Stack.push_back(CurrentModule); |
| 1075 | while (!Stack.empty()) { |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 1076 | Module *Mod = Stack.pop_back_val(); |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 1077 | |
Douglas Gregor | fb91265 | 2013-03-20 21:10:35 +0000 | [diff] [blame] | 1078 | // Resolve the exported declarations and conflicts. |
Douglas Gregor | 2b82c2a | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 1079 | // FIXME: Actually complain, once we figure out how to teach the |
Douglas Gregor | fb91265 | 2013-03-20 21:10:35 +0000 | [diff] [blame] | 1080 | // diagnostic client to deal with complaints in the module map at this |
Douglas Gregor | 2b82c2a | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 1081 | // point. |
| 1082 | ModMap.resolveExports(Mod, /*Complain=*/false); |
Daniel Jasper | ba7f2f7 | 2013-09-24 09:14:14 +0000 | [diff] [blame] | 1083 | ModMap.resolveUses(Mod, /*Complain=*/false); |
Douglas Gregor | fb91265 | 2013-03-20 21:10:35 +0000 | [diff] [blame] | 1084 | ModMap.resolveConflicts(Mod, /*Complain=*/false); |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 1085 | |
Douglas Gregor | 2b82c2a | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 1086 | // Queue the submodules, so their exports will also be resolved. |
Benjamin Kramer | f367dd9 | 2015-06-12 15:31:50 +0000 | [diff] [blame] | 1087 | Stack.append(Mod->submodule_begin(), Mod->submodule_end()); |
Douglas Gregor | 2b82c2a | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 1088 | } |
| 1089 | } |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 1090 | |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 1091 | // Warnings emitted in ActOnEndOfTranslationUnit() should be emitted for |
| 1092 | // modules when they are built, not every time they are used. |
| 1093 | emitAndClearUnusedLocalTypedefWarnings(); |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 1094 | } |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 1095 | |
Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 1096 | // C99 6.9.2p2: |
| 1097 | // A declaration of an identifier for an object that has file |
| 1098 | // scope without an initializer, and without a storage-class |
| 1099 | // specifier or with the storage-class specifier static, |
| 1100 | // constitutes a tentative definition. If a translation unit |
| 1101 | // contains one or more tentative definitions for an identifier, |
| 1102 | // and the translation unit contains no external definition for |
| 1103 | // that identifier, then the behavior is exactly as if the |
| 1104 | // translation unit contains a file scope declaration of that |
| 1105 | // identifier, with the composite type as of the end of the |
| 1106 | // translation unit, with an initializer equal to 0. |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1107 | llvm::SmallSet<VarDecl *, 32> Seen; |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 1108 | for (TentativeDefinitionsType::iterator |
Douglas Gregor | eb08bd4 | 2011-07-27 20:58:46 +0000 | [diff] [blame] | 1109 | T = TentativeDefinitions.begin(ExternalSource), |
| 1110 | TEnd = TentativeDefinitions.end(); |
Richard Smith | f5262c6 | 2018-06-28 01:57:04 +0000 | [diff] [blame] | 1111 | T != TEnd; ++T) { |
Douglas Gregor | eb08bd4 | 2011-07-27 20:58:46 +0000 | [diff] [blame] | 1112 | VarDecl *VD = (*T)->getActingDefinition(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1113 | |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1114 | // If the tentative definition was completed, getActingDefinition() returns |
| 1115 | // null. If we've already seen this variable before, insert()'s second |
| 1116 | // return value is false. |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 1117 | if (!VD || VD->isInvalidDecl() || !Seen.insert(VD).second) |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 1118 | continue; |
| 1119 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1120 | if (const IncompleteArrayType *ArrayT |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 1121 | = Context.getAsIncompleteArrayType(VD->getType())) { |
Chris Lattner | 0c79736 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 1122 | // Set the length of the array to 1 (C99 6.9.2p5). |
| 1123 | Diag(VD->getLocation(), diag::warn_tentative_incomplete_array); |
| 1124 | llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true); |
Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 1125 | QualType T = Context.getConstantArrayType(ArrayT->getElementType(), One, |
| 1126 | nullptr, ArrayType::Normal, 0); |
Chris Lattner | 0c79736 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 1127 | VD->setType(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1128 | } else if (RequireCompleteType(VD->getLocation(), VD->getType(), |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 1129 | diag::err_tentative_def_incomplete_type)) |
| 1130 | VD->setInvalidDecl(); |
| 1131 | |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 1132 | // No initialization is performed for a tentative definition. |
Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 1133 | CheckCompleteVariableDeclaration(VD); |
Eli Friedman | 7d14b3c | 2012-10-23 20:19:32 +0000 | [diff] [blame] | 1134 | |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 1135 | // Notify the consumer that we've completed a tentative definition. |
| 1136 | if (!VD->isInvalidDecl()) |
| 1137 | Consumer.CompleteTentativeDefinition(VD); |
Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 1138 | } |
Argyrios Kyrtzidis | 77fd99f | 2011-01-31 07:04:37 +0000 | [diff] [blame] | 1139 | |
| 1140 | // If there were errors, disable 'unused' warnings since they will mostly be |
Richard Smith | f5262c6 | 2018-06-28 01:57:04 +0000 | [diff] [blame] | 1141 | // noise. Don't warn for a use from a module: either we should warn on all |
| 1142 | // file-scope declarations in modules or not at all, but whether the |
| 1143 | // declaration is used is immaterial. |
| 1144 | if (!Diags.hasErrorOccurred() && TUKind != TU_Module) { |
Argyrios Kyrtzidis | 77fd99f | 2011-01-31 07:04:37 +0000 | [diff] [blame] | 1145 | // Output warning for unused file scoped decls. |
Douglas Gregor | a94a154 | 2011-07-27 21:45:57 +0000 | [diff] [blame] | 1146 | for (UnusedFileScopedDeclsType::iterator |
| 1147 | I = UnusedFileScopedDecls.begin(ExternalSource), |
Argyrios Kyrtzidis | 77fd99f | 2011-01-31 07:04:37 +0000 | [diff] [blame] | 1148 | E = UnusedFileScopedDecls.end(); I != E; ++I) { |
Douglas Gregor | a94a154 | 2011-07-27 21:45:57 +0000 | [diff] [blame] | 1149 | if (ShouldRemoveFromUnused(this, *I)) |
| 1150 | continue; |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 1151 | |
Argyrios Kyrtzidis | 77fd99f | 2011-01-31 07:04:37 +0000 | [diff] [blame] | 1152 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
| 1153 | const FunctionDecl *DiagD; |
| 1154 | if (!FD->hasBody(DiagD)) |
| 1155 | DiagD = FD; |
Argyrios Kyrtzidis | e028314 | 2011-03-03 17:47:42 +0000 | [diff] [blame] | 1156 | if (DiagD->isDeleted()) |
| 1157 | continue; // Deleted functions are supposed to be unused. |
Argyrios Kyrtzidis | 1618023 | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 1158 | if (DiagD->isReferenced()) { |
| 1159 | if (isa<CXXMethodDecl>(DiagD)) |
| 1160 | Diag(DiagD->getLocation(), diag::warn_unneeded_member_function) |
| 1161 | << DiagD->getDeclName(); |
Fariborz Jahanian | 91fc39e | 2012-06-27 19:43:29 +0000 | [diff] [blame] | 1162 | else { |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1163 | if (FD->getStorageClass() == SC_Static && |
Fariborz Jahanian | 91fc39e | 2012-06-27 19:43:29 +0000 | [diff] [blame] | 1164 | !FD->isInlineSpecified() && |
Eli Friedman | 5ba37d5 | 2013-08-22 00:27:10 +0000 | [diff] [blame] | 1165 | !SourceMgr.isInMainFile( |
Fariborz Jahanian | 91fc39e | 2012-06-27 19:43:29 +0000 | [diff] [blame] | 1166 | SourceMgr.getExpansionLoc(FD->getLocation()))) |
Nico Weber | 6381633 | 2014-07-26 23:20:08 +0000 | [diff] [blame] | 1167 | Diag(DiagD->getLocation(), |
| 1168 | diag::warn_unneeded_static_internal_decl) |
| 1169 | << DiagD->getDeclName(); |
Fariborz Jahanian | 91fc39e | 2012-06-27 19:43:29 +0000 | [diff] [blame] | 1170 | else |
| 1171 | Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl) |
| 1172 | << /*function*/0 << DiagD->getDeclName(); |
| 1173 | } |
Argyrios Kyrtzidis | 1618023 | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 1174 | } else { |
Vassil Vassilev | 64e1e1e | 2017-05-09 11:25:41 +0000 | [diff] [blame] | 1175 | if (FD->getDescribedFunctionTemplate()) |
| 1176 | Diag(DiagD->getLocation(), diag::warn_unused_template) |
| 1177 | << /*function*/0 << DiagD->getDeclName(); |
| 1178 | else |
| 1179 | Diag(DiagD->getLocation(), |
| 1180 | isa<CXXMethodDecl>(DiagD) ? diag::warn_unused_member_function |
| 1181 | : diag::warn_unused_function) |
| 1182 | << DiagD->getDeclName(); |
Argyrios Kyrtzidis | 1618023 | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 1183 | } |
Argyrios Kyrtzidis | 77fd99f | 2011-01-31 07:04:37 +0000 | [diff] [blame] | 1184 | } else { |
| 1185 | const VarDecl *DiagD = cast<VarDecl>(*I)->getDefinition(); |
| 1186 | if (!DiagD) |
| 1187 | DiagD = cast<VarDecl>(*I); |
Argyrios Kyrtzidis | 1618023 | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 1188 | if (DiagD->isReferenced()) { |
| 1189 | Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl) |
| 1190 | << /*variable*/1 << DiagD->getDeclName(); |
Daniel Jasper | c531dae | 2013-09-11 10:37:35 +0000 | [diff] [blame] | 1191 | } else if (DiagD->getType().isConstQualified()) { |
Erik Verbruggen | 89aa7eb | 2016-10-28 08:28:42 +0000 | [diff] [blame] | 1192 | const SourceManager &SM = SourceMgr; |
| 1193 | if (SM.getMainFileID() != SM.getFileID(DiagD->getLocation()) || |
| 1194 | !PP.getLangOpts().IsHeaderFile) |
| 1195 | Diag(DiagD->getLocation(), diag::warn_unused_const_variable) |
| 1196 | << DiagD->getDeclName(); |
Eli Friedman | 5ef2175 | 2013-09-10 03:05:56 +0000 | [diff] [blame] | 1197 | } else { |
Vassil Vassilev | 64e1e1e | 2017-05-09 11:25:41 +0000 | [diff] [blame] | 1198 | if (DiagD->getDescribedVarTemplate()) |
| 1199 | Diag(DiagD->getLocation(), diag::warn_unused_template) |
| 1200 | << /*variable*/1 << DiagD->getDeclName(); |
| 1201 | else |
| 1202 | Diag(DiagD->getLocation(), diag::warn_unused_variable) |
Matt Beaumont-Gay | e1368a1 | 2013-04-10 00:47:10 +0000 | [diff] [blame] | 1203 | << DiagD->getDeclName(); |
Argyrios Kyrtzidis | 1618023 | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 1204 | } |
Argyrios Kyrtzidis | 77fd99f | 2011-01-31 07:04:37 +0000 | [diff] [blame] | 1205 | } |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1206 | } |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 1207 | |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 1208 | emitAndClearUnusedLocalTypedefWarnings(); |
Argyrios Kyrtzidis | 35672e7 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 1209 | } |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1210 | |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 1211 | if (!Diags.isIgnored(diag::warn_unused_private_field, SourceLocation())) { |
Richard Smith | f5262c6 | 2018-06-28 01:57:04 +0000 | [diff] [blame] | 1212 | // FIXME: Load additional unused private field candidates from the external |
| 1213 | // source. |
Daniel Jasper | 0baec549 | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 1214 | RecordCompleteMap RecordsComplete; |
| 1215 | RecordCompleteMap MNCComplete; |
| 1216 | for (NamedDeclSetType::iterator I = UnusedPrivateFields.begin(), |
| 1217 | E = UnusedPrivateFields.end(); I != E; ++I) { |
| 1218 | const NamedDecl *D = *I; |
| 1219 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext()); |
| 1220 | if (RD && !RD->isUnion() && |
| 1221 | IsRecordFullyDefined(RD, RecordsComplete, MNCComplete)) { |
| 1222 | Diag(D->getLocation(), diag::warn_unused_private_field) |
| 1223 | << D->getDeclName(); |
| 1224 | } |
| 1225 | } |
| 1226 | } |
| 1227 | |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 1228 | if (!Diags.isIgnored(diag::warn_mismatched_delete_new, SourceLocation())) { |
| 1229 | if (ExternalSource) |
| 1230 | ExternalSource->ReadMismatchingDeleteExpressions(DeleteExprs); |
| 1231 | for (const auto &DeletedFieldInfo : DeleteExprs) { |
| 1232 | for (const auto &DeleteExprLoc : DeletedFieldInfo.second) { |
| 1233 | AnalyzeDeleteExprMismatch(DeletedFieldInfo.first, DeleteExprLoc.first, |
| 1234 | DeleteExprLoc.second); |
| 1235 | } |
| 1236 | } |
| 1237 | } |
| 1238 | |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1239 | // Check we've noticed that we're no longer parsing the initializer for every |
| 1240 | // variable. If we miss cases, then at best we have a performance issue and |
| 1241 | // at worst a rejects-valid bug. |
| 1242 | assert(ParsingInitForAutoVars.empty() && |
| 1243 | "Didn't unmark var as having its initializer parsed"); |
| 1244 | |
Vassil Vassilev | 45bf62f | 2016-10-17 10:15:25 +0000 | [diff] [blame] | 1245 | if (!PP.isIncrementalProcessingEnabled()) |
| 1246 | TUScope = nullptr; |
Chris Lattner | f440440 | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 1250 | //===----------------------------------------------------------------------===// |
Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 1251 | // Helper functions. |
| 1252 | //===----------------------------------------------------------------------===// |
| 1253 | |
Anders Carlsson | b26ab81 | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 1254 | DeclContext *Sema::getFunctionLevelDeclContext() { |
John McCall | b878801 | 2009-12-19 10:53:49 +0000 | [diff] [blame] | 1255 | DeclContext *DC = CurContext; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1256 | |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1257 | while (true) { |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 1258 | if (isa<BlockDecl>(DC) || isa<EnumDecl>(DC) || isa<CapturedDecl>(DC)) { |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1259 | DC = DC->getParent(); |
| 1260 | } else if (isa<CXXMethodDecl>(DC) && |
Douglas Gregor | 1a22d28 | 2012-02-12 17:34:23 +0000 | [diff] [blame] | 1261 | cast<CXXMethodDecl>(DC)->getOverloadedOperator() == OO_Call && |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1262 | cast<CXXRecordDecl>(DC->getParent())->isLambda()) { |
| 1263 | DC = DC->getParent()->getParent(); |
| 1264 | } |
| 1265 | else break; |
| 1266 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1267 | |
Anders Carlsson | b26ab81 | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 1268 | return DC; |
| 1269 | } |
| 1270 | |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 1271 | /// getCurFunctionDecl - If inside of a function body, this returns a pointer |
| 1272 | /// to the function decl for the function being parsed. If we're currently |
| 1273 | /// in a 'block', this returns the containing context. |
| 1274 | FunctionDecl *Sema::getCurFunctionDecl() { |
Anders Carlsson | b26ab81 | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 1275 | DeclContext *DC = getFunctionLevelDeclContext(); |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 1276 | return dyn_cast<FunctionDecl>(DC); |
| 1277 | } |
| 1278 | |
Daniel Dunbar | 6e8aa53 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 1279 | ObjCMethodDecl *Sema::getCurMethodDecl() { |
Anders Carlsson | b26ab81 | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 1280 | DeclContext *DC = getFunctionLevelDeclContext(); |
Fariborz Jahanian | deac9ac | 2013-05-31 21:51:12 +0000 | [diff] [blame] | 1281 | while (isa<RecordDecl>(DC)) |
| 1282 | DC = DC->getParent(); |
Steve Naroff | ecf2bb8 | 2008-11-17 16:28:52 +0000 | [diff] [blame] | 1283 | return dyn_cast<ObjCMethodDecl>(DC); |
Daniel Dunbar | 6e8aa53 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 1284 | } |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 1285 | |
| 1286 | NamedDecl *Sema::getCurFunctionOrMethodDecl() { |
Anders Carlsson | b26ab81 | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 1287 | DeclContext *DC = getFunctionLevelDeclContext(); |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 1288 | if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC)) |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1289 | return cast<NamedDecl>(DC); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1290 | return nullptr; |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Anastasia Stulova | 980133a | 2019-12-03 12:55:50 +0000 | [diff] [blame^] | 1293 | LangAS Sema::getDefaultCXXMethodAddrSpace() const { |
| 1294 | if (getLangOpts().OpenCL) |
| 1295 | return LangAS::opencl_generic; |
| 1296 | return LangAS::Default; |
| 1297 | } |
| 1298 | |
Daniel Dunbar | d671ab9 | 2012-03-14 09:49:32 +0000 | [diff] [blame] | 1299 | void Sema::EmitCurrentDiagnostic(unsigned DiagID) { |
| 1300 | // FIXME: It doesn't make sense to me that DiagID is an incoming argument here |
| 1301 | // and yet we also use the current diag ID on the DiagnosticsEngine. This has |
| 1302 | // been made more painfully obvious by the refactor that introduced this |
| 1303 | // function, but it is possible that the incoming argument can be |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 1304 | // eliminated. If it truly cannot be (for example, there is some reentrancy |
Daniel Dunbar | d671ab9 | 2012-03-14 09:49:32 +0000 | [diff] [blame] | 1305 | // issue I am not seeing yet), then there should at least be a clarifying |
| 1306 | // comment somewhere. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1307 | if (Optional<TemplateDeductionInfo*> Info = isSFINAEContext()) { |
Daniel Dunbar | d671ab9 | 2012-03-14 09:49:32 +0000 | [diff] [blame] | 1308 | switch (DiagnosticIDs::getDiagnosticSFINAEResponse( |
| 1309 | Diags.getCurrentDiagID())) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 1310 | case DiagnosticIDs::SFINAE_Report: |
Richard Smith | 38c0e04 | 2011-10-19 00:07:01 +0000 | [diff] [blame] | 1311 | // We'll report the diagnostic below. |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1312 | break; |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 1313 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 1314 | case DiagnosticIDs::SFINAE_SubstitutionFailure: |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1315 | // Count this failure so that we know that template argument deduction |
| 1316 | // has failed. |
Daniel Dunbar | d671ab9 | 2012-03-14 09:49:32 +0000 | [diff] [blame] | 1317 | ++NumSFINAEErrors; |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 1318 | |
| 1319 | // Make a copy of this suppressed diagnostic and store it with the |
| 1320 | // template-deduction information. |
| 1321 | if (*Info && !(*Info)->hasSFINAEDiagnostic()) { |
| 1322 | Diagnostic DiagInfo(&Diags); |
| 1323 | (*Info)->addSFINAEDiagnostic(DiagInfo.getLocation(), |
| 1324 | PartialDiagnostic(DiagInfo, Context.getDiagAllocator())); |
| 1325 | } |
| 1326 | |
Dmitri Gribenko | 20f45ed | 2019-09-12 12:16:43 +0000 | [diff] [blame] | 1327 | Diags.setLastDiagnosticIgnored(true); |
Daniel Dunbar | d671ab9 | 2012-03-14 09:49:32 +0000 | [diff] [blame] | 1328 | Diags.Clear(); |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1329 | return; |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 1330 | |
Richard Smith | 38c0e04 | 2011-10-19 00:07:01 +0000 | [diff] [blame] | 1331 | case DiagnosticIDs::SFINAE_AccessControl: { |
| 1332 | // Per C++ Core Issue 1170, access control is part of SFINAE. |
Daniel Dunbar | a25002f | 2012-03-13 18:30:54 +0000 | [diff] [blame] | 1333 | // Additionally, the AccessCheckingSFINAE flag can be used to temporarily |
Richard Smith | 38c0e04 | 2011-10-19 00:07:01 +0000 | [diff] [blame] | 1334 | // make access control a part of SFINAE for the purposes of checking |
| 1335 | // type traits. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1336 | if (!AccessCheckingSFINAE && !getLangOpts().CPlusPlus11) |
Richard Smith | 38c0e04 | 2011-10-19 00:07:01 +0000 | [diff] [blame] | 1337 | break; |
| 1338 | |
Daniel Dunbar | d671ab9 | 2012-03-14 09:49:32 +0000 | [diff] [blame] | 1339 | SourceLocation Loc = Diags.getCurrentDiagLoc(); |
Richard Smith | 38c0e04 | 2011-10-19 00:07:01 +0000 | [diff] [blame] | 1340 | |
| 1341 | // Suppress this diagnostic. |
Daniel Dunbar | d671ab9 | 2012-03-14 09:49:32 +0000 | [diff] [blame] | 1342 | ++NumSFINAEErrors; |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 1343 | |
| 1344 | // Make a copy of this suppressed diagnostic and store it with the |
| 1345 | // template-deduction information. |
| 1346 | if (*Info && !(*Info)->hasSFINAEDiagnostic()) { |
| 1347 | Diagnostic DiagInfo(&Diags); |
| 1348 | (*Info)->addSFINAEDiagnostic(DiagInfo.getLocation(), |
| 1349 | PartialDiagnostic(DiagInfo, Context.getDiagAllocator())); |
| 1350 | } |
| 1351 | |
Dmitri Gribenko | 20f45ed | 2019-09-12 12:16:43 +0000 | [diff] [blame] | 1352 | Diags.setLastDiagnosticIgnored(true); |
Daniel Dunbar | d671ab9 | 2012-03-14 09:49:32 +0000 | [diff] [blame] | 1353 | Diags.Clear(); |
Richard Smith | 38c0e04 | 2011-10-19 00:07:01 +0000 | [diff] [blame] | 1354 | |
| 1355 | // Now the diagnostic state is clear, produce a C++98 compatibility |
| 1356 | // warning. |
Daniel Dunbar | d671ab9 | 2012-03-14 09:49:32 +0000 | [diff] [blame] | 1357 | Diag(Loc, diag::warn_cxx98_compat_sfinae_access_control); |
Richard Smith | 38c0e04 | 2011-10-19 00:07:01 +0000 | [diff] [blame] | 1358 | |
| 1359 | // The last diagnostic which Sema produced was ignored. Suppress any |
| 1360 | // notes attached to it. |
Dmitri Gribenko | 20f45ed | 2019-09-12 12:16:43 +0000 | [diff] [blame] | 1361 | Diags.setLastDiagnosticIgnored(true); |
Richard Smith | 38c0e04 | 2011-10-19 00:07:01 +0000 | [diff] [blame] | 1362 | return; |
| 1363 | } |
| 1364 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 1365 | case DiagnosticIDs::SFINAE_Suppress: |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1366 | // Make a copy of this suppressed diagnostic and store it with the |
| 1367 | // template-deduction information; |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 1368 | if (*Info) { |
| 1369 | Diagnostic DiagInfo(&Diags); |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 1370 | (*Info)->addSuppressedDiagnostic(DiagInfo.getLocation(), |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 1371 | PartialDiagnostic(DiagInfo, Context.getDiagAllocator())); |
| 1372 | } |
| 1373 | |
| 1374 | // Suppress this diagnostic. |
Dmitri Gribenko | 20f45ed | 2019-09-12 12:16:43 +0000 | [diff] [blame] | 1375 | Diags.setLastDiagnosticIgnored(true); |
Daniel Dunbar | d671ab9 | 2012-03-14 09:49:32 +0000 | [diff] [blame] | 1376 | Diags.Clear(); |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1377 | return; |
| 1378 | } |
| 1379 | } |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 1380 | |
Joel E. Denny | 7bcc210 | 2018-05-14 18:41:44 +0000 | [diff] [blame] | 1381 | // Copy the diagnostic printing policy over the ASTContext printing policy. |
| 1382 | // TODO: Stop doing that. See: https://reviews.llvm.org/D45093#1090292 |
Daniel Dunbar | d671ab9 | 2012-03-14 09:49:32 +0000 | [diff] [blame] | 1383 | Context.setPrintingPolicy(getPrintingPolicy()); |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 1384 | |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1385 | // Emit the diagnostic. |
Daniel Dunbar | d671ab9 | 2012-03-14 09:49:32 +0000 | [diff] [blame] | 1386 | if (!Diags.EmitCurrentDiagnostic()) |
Douglas Gregor | 3383451 | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 1387 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1388 | |
Douglas Gregor | da17bd3 | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 1389 | // If this is not a note, and we're in a template instantiation |
| 1390 | // that is different from the last template instantiation where |
| 1391 | // we emitted an error, print a template instantiation |
| 1392 | // backtrace. |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 1393 | if (!DiagnosticIDs::isBuiltinNote(DiagID)) |
| 1394 | PrintContextStack(); |
Douglas Gregor | da17bd3 | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 1395 | } |
Douglas Gregor | c6d5edd | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 1396 | |
Anders Carlsson | f68079e | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 1397 | Sema::SemaDiagnosticBuilder |
| 1398 | Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) { |
| 1399 | SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID())); |
| 1400 | PD.Emit(Builder); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1401 | |
Anders Carlsson | f68079e | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 1402 | return Builder; |
| 1403 | } |
| 1404 | |
Alexey Bataev | 8972133 | 2019-02-07 19:46:42 +0000 | [diff] [blame] | 1405 | // Print notes showing how we can reach FD starting from an a priori |
| 1406 | // known-callable function. |
| 1407 | static void emitCallStackNotes(Sema &S, FunctionDecl *FD) { |
| 1408 | auto FnIt = S.DeviceKnownEmittedFns.find(FD); |
| 1409 | while (FnIt != S.DeviceKnownEmittedFns.end()) { |
| 1410 | DiagnosticBuilder Builder( |
| 1411 | S.Diags.Report(FnIt->second.Loc, diag::note_called_by)); |
| 1412 | Builder << FnIt->second.FD; |
| 1413 | Builder.setForceEmit(); |
| 1414 | |
| 1415 | FnIt = S.DeviceKnownEmittedFns.find(FnIt->second.FD); |
| 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | // Emit any deferred diagnostics for FD and erase them from the map in which |
| 1420 | // they're stored. |
Alexey Bataev | 9fd495b | 2019-08-20 19:50:13 +0000 | [diff] [blame] | 1421 | static void emitDeferredDiags(Sema &S, FunctionDecl *FD, bool ShowCallStack) { |
Alexey Bataev | 8972133 | 2019-02-07 19:46:42 +0000 | [diff] [blame] | 1422 | auto It = S.DeviceDeferredDiags.find(FD); |
| 1423 | if (It == S.DeviceDeferredDiags.end()) |
| 1424 | return; |
| 1425 | bool HasWarningOrError = false; |
| 1426 | for (PartialDiagnosticAt &PDAt : It->second) { |
| 1427 | const SourceLocation &Loc = PDAt.first; |
| 1428 | const PartialDiagnostic &PD = PDAt.second; |
| 1429 | HasWarningOrError |= S.getDiagnostics().getDiagnosticLevel( |
| 1430 | PD.getDiagID(), Loc) >= DiagnosticsEngine::Warning; |
| 1431 | DiagnosticBuilder Builder(S.Diags.Report(Loc, PD.getDiagID())); |
| 1432 | Builder.setForceEmit(); |
| 1433 | PD.Emit(Builder); |
| 1434 | } |
| 1435 | S.DeviceDeferredDiags.erase(It); |
| 1436 | |
| 1437 | // FIXME: Should this be called after every warning/error emitted in the loop |
| 1438 | // above, instead of just once per function? That would be consistent with |
| 1439 | // how we handle immediate errors, but it also seems like a bit much. |
Alexey Bataev | 9fd495b | 2019-08-20 19:50:13 +0000 | [diff] [blame] | 1440 | if (HasWarningOrError && ShowCallStack) |
Alexey Bataev | 8972133 | 2019-02-07 19:46:42 +0000 | [diff] [blame] | 1441 | emitCallStackNotes(S, FD); |
| 1442 | } |
| 1443 | |
| 1444 | // In CUDA, there are some constructs which may appear in semantically-valid |
| 1445 | // code, but trigger errors if we ever generate code for the function in which |
| 1446 | // they appear. Essentially every construct you're not allowed to use on the |
| 1447 | // device falls into this category, because you are allowed to use these |
| 1448 | // constructs in a __host__ __device__ function, but only if that function is |
| 1449 | // never codegen'ed on the device. |
| 1450 | // |
| 1451 | // To handle semantic checking for these constructs, we keep track of the set of |
| 1452 | // functions we know will be emitted, either because we could tell a priori that |
| 1453 | // they would be emitted, or because they were transitively called by a |
| 1454 | // known-emitted function. |
| 1455 | // |
| 1456 | // We also keep a partial call graph of which not-known-emitted functions call |
| 1457 | // which other not-known-emitted functions. |
| 1458 | // |
| 1459 | // When we see something which is illegal if the current function is emitted |
| 1460 | // (usually by way of CUDADiagIfDeviceCode, CUDADiagIfHostCode, or |
| 1461 | // CheckCUDACall), we first check if the current function is known-emitted. If |
| 1462 | // so, we immediately output the diagnostic. |
| 1463 | // |
| 1464 | // Otherwise, we "defer" the diagnostic. It sits in Sema::DeviceDeferredDiags |
| 1465 | // until we discover that the function is known-emitted, at which point we take |
| 1466 | // it out of this map and emit the diagnostic. |
| 1467 | |
| 1468 | Sema::DeviceDiagBuilder::DeviceDiagBuilder(Kind K, SourceLocation Loc, |
| 1469 | unsigned DiagID, FunctionDecl *Fn, |
| 1470 | Sema &S) |
| 1471 | : S(S), Loc(Loc), DiagID(DiagID), Fn(Fn), |
| 1472 | ShowCallStack(K == K_ImmediateWithCallStack || K == K_Deferred) { |
| 1473 | switch (K) { |
| 1474 | case K_Nop: |
| 1475 | break; |
| 1476 | case K_Immediate: |
| 1477 | case K_ImmediateWithCallStack: |
| 1478 | ImmediateDiag.emplace(S.Diag(Loc, DiagID)); |
| 1479 | break; |
| 1480 | case K_Deferred: |
| 1481 | assert(Fn && "Must have a function to attach the deferred diag to."); |
Alexey Bataev | 3167b30 | 2019-02-22 14:42:48 +0000 | [diff] [blame] | 1482 | auto &Diags = S.DeviceDeferredDiags[Fn]; |
| 1483 | PartialDiagId.emplace(Diags.size()); |
| 1484 | Diags.emplace_back(Loc, S.PDiag(DiagID)); |
Alexey Bataev | 8972133 | 2019-02-07 19:46:42 +0000 | [diff] [blame] | 1485 | break; |
| 1486 | } |
| 1487 | } |
| 1488 | |
Alexey Bataev | e69f94e | 2019-02-22 20:36:10 +0000 | [diff] [blame] | 1489 | Sema::DeviceDiagBuilder::DeviceDiagBuilder(DeviceDiagBuilder &&D) |
| 1490 | : S(D.S), Loc(D.Loc), DiagID(D.DiagID), Fn(D.Fn), |
| 1491 | ShowCallStack(D.ShowCallStack), ImmediateDiag(D.ImmediateDiag), |
| 1492 | PartialDiagId(D.PartialDiagId) { |
| 1493 | // Clean the previous diagnostics. |
| 1494 | D.ShowCallStack = false; |
| 1495 | D.ImmediateDiag.reset(); |
| 1496 | D.PartialDiagId.reset(); |
| 1497 | } |
| 1498 | |
Alexey Bataev | 8972133 | 2019-02-07 19:46:42 +0000 | [diff] [blame] | 1499 | Sema::DeviceDiagBuilder::~DeviceDiagBuilder() { |
| 1500 | if (ImmediateDiag) { |
| 1501 | // Emit our diagnostic and, if it was a warning or error, output a callstack |
| 1502 | // if Fn isn't a priori known-emitted. |
| 1503 | bool IsWarningOrError = S.getDiagnostics().getDiagnosticLevel( |
| 1504 | DiagID, Loc) >= DiagnosticsEngine::Warning; |
| 1505 | ImmediateDiag.reset(); // Emit the immediate diag. |
| 1506 | if (IsWarningOrError && ShowCallStack) |
| 1507 | emitCallStackNotes(S, Fn); |
Alexey Bataev | 3167b30 | 2019-02-22 14:42:48 +0000 | [diff] [blame] | 1508 | } else { |
| 1509 | assert((!PartialDiagId || ShowCallStack) && |
| 1510 | "Must always show call stack for deferred diags."); |
Alexey Bataev | 8972133 | 2019-02-07 19:46:42 +0000 | [diff] [blame] | 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | // Indicate that this function (and thus everything it transtively calls) will |
| 1515 | // be codegen'ed, and emit any deferred diagnostics on this function and its |
| 1516 | // (transitive) callees. |
| 1517 | void Sema::markKnownEmitted( |
| 1518 | Sema &S, FunctionDecl *OrigCaller, FunctionDecl *OrigCallee, |
| 1519 | SourceLocation OrigLoc, |
| 1520 | const llvm::function_ref<bool(Sema &, FunctionDecl *)> IsKnownEmitted) { |
| 1521 | // Nothing to do if we already know that FD is emitted. |
| 1522 | if (IsKnownEmitted(S, OrigCallee)) { |
| 1523 | assert(!S.DeviceCallGraph.count(OrigCallee)); |
| 1524 | return; |
| 1525 | } |
| 1526 | |
| 1527 | // We've just discovered that OrigCallee is known-emitted. Walk our call |
| 1528 | // graph to see what else we can now discover also must be emitted. |
| 1529 | |
| 1530 | struct CallInfo { |
| 1531 | FunctionDecl *Caller; |
| 1532 | FunctionDecl *Callee; |
| 1533 | SourceLocation Loc; |
| 1534 | }; |
| 1535 | llvm::SmallVector<CallInfo, 4> Worklist = {{OrigCaller, OrigCallee, OrigLoc}}; |
| 1536 | llvm::SmallSet<CanonicalDeclPtr<FunctionDecl>, 4> Seen; |
| 1537 | Seen.insert(OrigCallee); |
| 1538 | while (!Worklist.empty()) { |
| 1539 | CallInfo C = Worklist.pop_back_val(); |
| 1540 | assert(!IsKnownEmitted(S, C.Callee) && |
| 1541 | "Worklist should not contain known-emitted functions."); |
| 1542 | S.DeviceKnownEmittedFns[C.Callee] = {C.Caller, C.Loc}; |
Alexey Bataev | 9fd495b | 2019-08-20 19:50:13 +0000 | [diff] [blame] | 1543 | emitDeferredDiags(S, C.Callee, C.Caller); |
Alexey Bataev | 8972133 | 2019-02-07 19:46:42 +0000 | [diff] [blame] | 1544 | |
| 1545 | // If this is a template instantiation, explore its callgraph as well: |
| 1546 | // Non-dependent calls are part of the template's callgraph, while dependent |
| 1547 | // calls are part of to the instantiation's call graph. |
| 1548 | if (auto *Templ = C.Callee->getPrimaryTemplate()) { |
| 1549 | FunctionDecl *TemplFD = Templ->getAsFunction(); |
| 1550 | if (!Seen.count(TemplFD) && !S.DeviceKnownEmittedFns.count(TemplFD)) { |
| 1551 | Seen.insert(TemplFD); |
| 1552 | Worklist.push_back( |
| 1553 | {/* Caller = */ C.Caller, /* Callee = */ TemplFD, C.Loc}); |
| 1554 | } |
| 1555 | } |
| 1556 | |
| 1557 | // Add all functions called by Callee to our worklist. |
| 1558 | auto CGIt = S.DeviceCallGraph.find(C.Callee); |
| 1559 | if (CGIt == S.DeviceCallGraph.end()) |
| 1560 | continue; |
| 1561 | |
| 1562 | for (std::pair<CanonicalDeclPtr<FunctionDecl>, SourceLocation> FDLoc : |
| 1563 | CGIt->second) { |
| 1564 | FunctionDecl *NewCallee = FDLoc.first; |
| 1565 | SourceLocation CallLoc = FDLoc.second; |
| 1566 | if (Seen.count(NewCallee) || IsKnownEmitted(S, NewCallee)) |
| 1567 | continue; |
| 1568 | Seen.insert(NewCallee); |
| 1569 | Worklist.push_back( |
| 1570 | {/* Caller = */ C.Callee, /* Callee = */ NewCallee, CallLoc}); |
| 1571 | } |
| 1572 | |
| 1573 | // C.Callee is now known-emitted, so we no longer need to maintain its list |
| 1574 | // of callees in DeviceCallGraph. |
| 1575 | S.DeviceCallGraph.erase(CGIt); |
| 1576 | } |
| 1577 | } |
| 1578 | |
Alexey Bataev | 3167b30 | 2019-02-22 14:42:48 +0000 | [diff] [blame] | 1579 | Sema::DeviceDiagBuilder Sema::targetDiag(SourceLocation Loc, unsigned DiagID) { |
Alexey Bataev | 729e242 | 2019-08-23 16:11:14 +0000 | [diff] [blame] | 1580 | if (LangOpts.OpenMP) |
| 1581 | return LangOpts.OpenMPIsDevice ? diagIfOpenMPDeviceCode(Loc, DiagID) |
| 1582 | : diagIfOpenMPHostCode(Loc, DiagID); |
Alexey Bataev | 3167b30 | 2019-02-22 14:42:48 +0000 | [diff] [blame] | 1583 | if (getLangOpts().CUDA) |
| 1584 | return getLangOpts().CUDAIsDevice ? CUDADiagIfDeviceCode(Loc, DiagID) |
| 1585 | : CUDADiagIfHostCode(Loc, DiagID); |
Alexey Bataev | 5c96c1c | 2019-02-20 17:42:57 +0000 | [diff] [blame] | 1586 | return DeviceDiagBuilder(DeviceDiagBuilder::K_Immediate, Loc, DiagID, |
| 1587 | getCurFunctionDecl(), *this); |
| 1588 | } |
| 1589 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1590 | /// Looks through the macro-expansion chain for the given |
Chandler Carruth | c22845a | 2011-07-26 05:40:03 +0000 | [diff] [blame] | 1591 | /// location, looking for a macro expansion with the given name. |
John McCall | 462c055 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 1592 | /// If one is found, returns true and sets the location to that |
Chandler Carruth | c22845a | 2011-07-26 05:40:03 +0000 | [diff] [blame] | 1593 | /// expansion loc. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1594 | bool Sema::findMacroSpelling(SourceLocation &locref, StringRef name) { |
John McCall | 462c055 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 1595 | SourceLocation loc = locref; |
| 1596 | if (!loc.isMacroID()) return false; |
| 1597 | |
| 1598 | // There's no good way right now to look at the intermediate |
Chandler Carruth | c22845a | 2011-07-26 05:40:03 +0000 | [diff] [blame] | 1599 | // expansions, so just jump to the expansion location. |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 1600 | loc = getSourceManager().getExpansionLoc(loc); |
John McCall | 462c055 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 1601 | |
| 1602 | // If that's written with the name, stop here. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1603 | SmallVector<char, 16> buffer; |
John McCall | 462c055 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 1604 | if (getPreprocessor().getSpelling(loc, buffer) == name) { |
| 1605 | locref = loc; |
| 1606 | return true; |
| 1607 | } |
| 1608 | return false; |
| 1609 | } |
| 1610 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1611 | /// Determines the active Scope associated with the given declaration |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1612 | /// context. |
| 1613 | /// |
| 1614 | /// This routine maps a declaration context to the active Scope object that |
| 1615 | /// represents that declaration context in the parser. It is typically used |
| 1616 | /// from "scope-less" code (e.g., template instantiation, lazy creation of |
| 1617 | /// declarations) that injects a name for name-lookup purposes and, therefore, |
| 1618 | /// must update the Scope. |
| 1619 | /// |
| 1620 | /// \returns The scope corresponding to the given declaraion context, or NULL |
| 1621 | /// if no such scope is open. |
| 1622 | Scope *Sema::getScopeForContext(DeclContext *Ctx) { |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 1623 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1624 | if (!Ctx) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1625 | return nullptr; |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 1626 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1627 | Ctx = Ctx->getPrimaryContext(); |
| 1628 | for (Scope *S = getCurScope(); S; S = S->getParent()) { |
Sebastian Redl | caef9ab | 2010-07-08 23:07:34 +0000 | [diff] [blame] | 1629 | // Ignore scopes that cannot have declarations. This is important for |
| 1630 | // out-of-line definitions of static class members. |
| 1631 | if (S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope)) |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 1632 | if (DeclContext *Entity = S->getEntity()) |
Sebastian Redl | caef9ab | 2010-07-08 23:07:34 +0000 | [diff] [blame] | 1633 | if (Ctx == Entity->getPrimaryContext()) |
| 1634 | return S; |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1635 | } |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 1636 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1637 | return nullptr; |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1638 | } |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1639 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1640 | /// Enter a new function scope |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1641 | void Sema::PushFunctionScope() { |
Richard Smith | 2fdd95c | 2019-05-31 00:45:09 +0000 | [diff] [blame] | 1642 | if (FunctionScopes.empty() && CachedFunctionScope) { |
| 1643 | // Use CachedFunctionScope to avoid allocating memory when possible. |
| 1644 | CachedFunctionScope->Clear(); |
| 1645 | FunctionScopes.push_back(CachedFunctionScope.release()); |
Reid Kleckner | 87a3180 | 2018-03-12 21:43:02 +0000 | [diff] [blame] | 1646 | } else { |
| 1647 | FunctionScopes.push_back(new FunctionScopeInfo(getDiagnostics())); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1648 | } |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 1649 | if (LangOpts.OpenMP) |
| 1650 | pushOpenMPFunctionRegion(); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | void Sema::PushBlockScope(Scope *BlockScope, BlockDecl *Block) { |
Argyrios Kyrtzidis | f51ec1d | 2010-11-19 00:19:15 +0000 | [diff] [blame] | 1654 | FunctionScopes.push_back(new BlockScopeInfo(getDiagnostics(), |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1655 | BlockScope, Block)); |
| 1656 | } |
| 1657 | |
Faisal Vali | 97d8c33 | 2013-11-12 01:46:33 +0000 | [diff] [blame] | 1658 | LambdaScopeInfo *Sema::PushLambdaScope() { |
Faisal Vali | 524ca28 | 2013-11-12 01:40:44 +0000 | [diff] [blame] | 1659 | LambdaScopeInfo *const LSI = new LambdaScopeInfo(getDiagnostics()); |
| 1660 | FunctionScopes.push_back(LSI); |
| 1661 | return LSI; |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | void Sema::RecordParsingTemplateParameterDepth(unsigned Depth) { |
| 1665 | if (LambdaScopeInfo *const LSI = getCurLambda()) { |
| 1666 | LSI->AutoTemplateParameterDepth = Depth; |
| 1667 | return; |
Reid Kleckner | 87a3180 | 2018-03-12 21:43:02 +0000 | [diff] [blame] | 1668 | } |
| 1669 | llvm_unreachable( |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 1670 | "Remove assertion if intentionally called in a non-lambda context."); |
Eli Friedman | 71c8055 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
Akira Hatanaka | 8e57b07 | 2018-10-01 21:51:28 +0000 | [diff] [blame] | 1673 | // Check that the type of the VarDecl has an accessible copy constructor and |
JF Bastien | ea51a8c | 2019-04-30 00:19:43 +0000 | [diff] [blame] | 1674 | // resolve its destructor's exception specification. |
Akira Hatanaka | 8e57b07 | 2018-10-01 21:51:28 +0000 | [diff] [blame] | 1675 | static void checkEscapingByref(VarDecl *VD, Sema &S) { |
| 1676 | QualType T = VD->getType(); |
| 1677 | EnterExpressionEvaluationContext scope( |
| 1678 | S, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); |
| 1679 | SourceLocation Loc = VD->getLocation(); |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 1680 | Expr *VarRef = |
| 1681 | new (S.Context) DeclRefExpr(S.Context, VD, false, T, VK_LValue, Loc); |
Akira Hatanaka | 8e57b07 | 2018-10-01 21:51:28 +0000 | [diff] [blame] | 1682 | ExprResult Result = S.PerformMoveOrCopyInitialization( |
| 1683 | InitializedEntity::InitializeBlock(Loc, T, false), VD, VD->getType(), |
| 1684 | VarRef, /*AllowNRVO=*/true); |
| 1685 | if (!Result.isInvalid()) { |
| 1686 | Result = S.MaybeCreateExprWithCleanups(Result); |
| 1687 | Expr *Init = Result.getAs<Expr>(); |
| 1688 | S.Context.setBlockVarCopyInit(VD, Init, S.canThrow(Init)); |
| 1689 | } |
| 1690 | |
JF Bastien | ea51a8c | 2019-04-30 00:19:43 +0000 | [diff] [blame] | 1691 | // The destructor's exception specification is needed when IRGen generates |
Akira Hatanaka | 8e57b07 | 2018-10-01 21:51:28 +0000 | [diff] [blame] | 1692 | // block copy/destroy functions. Resolve it here. |
| 1693 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 1694 | if (CXXDestructorDecl *DD = RD->getDestructor()) { |
| 1695 | auto *FPT = DD->getType()->getAs<FunctionProtoType>(); |
| 1696 | S.ResolveExceptionSpec(Loc, FPT); |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | static void markEscapingByrefs(const FunctionScopeInfo &FSI, Sema &S) { |
| 1701 | // Set the EscapingByref flag of __block variables captured by |
| 1702 | // escaping blocks. |
| 1703 | for (const BlockDecl *BD : FSI.Blocks) { |
Akira Hatanaka | 8e57b07 | 2018-10-01 21:51:28 +0000 | [diff] [blame] | 1704 | for (const BlockDecl::Capture &BC : BD->captures()) { |
| 1705 | VarDecl *VD = BC.getVariable(); |
Akira Hatanaka | 0905106 | 2019-09-07 00:34:43 +0000 | [diff] [blame] | 1706 | if (VD->hasAttr<BlocksAttr>()) { |
| 1707 | // Nothing to do if this is a __block variable captured by a |
| 1708 | // non-escaping block. |
| 1709 | if (BD->doesNotEscape()) |
| 1710 | continue; |
Akira Hatanaka | 8e57b07 | 2018-10-01 21:51:28 +0000 | [diff] [blame] | 1711 | VD->setEscapingByref(); |
Akira Hatanaka | 0905106 | 2019-09-07 00:34:43 +0000 | [diff] [blame] | 1712 | } |
| 1713 | // Check whether the captured variable is or contains an object of |
| 1714 | // non-trivial C union type. |
| 1715 | QualType CapType = BC.getVariable()->getType(); |
| 1716 | if (CapType.hasNonTrivialToPrimitiveDestructCUnion() || |
| 1717 | CapType.hasNonTrivialToPrimitiveCopyCUnion()) |
| 1718 | S.checkNonTrivialCUnion(BC.getVariable()->getType(), |
| 1719 | BD->getCaretLocation(), |
| 1720 | Sema::NTCUC_BlockCapture, |
| 1721 | Sema::NTCUK_Destruct|Sema::NTCUK_Copy); |
Akira Hatanaka | 8e57b07 | 2018-10-01 21:51:28 +0000 | [diff] [blame] | 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | for (VarDecl *VD : FSI.ByrefBlockVars) { |
| 1726 | // __block variables might require us to capture a copy-initializer. |
| 1727 | if (!VD->isEscapingByref()) |
| 1728 | continue; |
| 1729 | // It's currently invalid to ever have a __block variable with an |
| 1730 | // array type; should we diagnose that here? |
| 1731 | // Regardless, we don't want to ignore array nesting when |
| 1732 | // constructing this copy. |
| 1733 | if (VD->getType()->isStructureOrClassType()) |
| 1734 | checkEscapingByref(VD, S); |
| 1735 | } |
| 1736 | } |
| 1737 | |
Richard Smith | 2fdd95c | 2019-05-31 00:45:09 +0000 | [diff] [blame] | 1738 | /// Pop a function (or block or lambda or captured region) scope from the stack. |
| 1739 | /// |
| 1740 | /// \param WP The warning policy to use for CFG-based warnings, or null if such |
| 1741 | /// warnings should not be produced. |
| 1742 | /// \param D The declaration corresponding to this function scope, if producing |
| 1743 | /// CFG-based warnings. |
| 1744 | /// \param BlockType The type of the block expression, if D is a BlockDecl. |
| 1745 | Sema::PoppedFunctionScopePtr |
| 1746 | Sema::PopFunctionScopeInfo(const AnalysisBasedWarnings::Policy *WP, |
| 1747 | const Decl *D, QualType BlockType) { |
Reid Kleckner | 8d485b8 | 2018-03-08 01:12:22 +0000 | [diff] [blame] | 1748 | assert(!FunctionScopes.empty() && "mismatched push/pop!"); |
Akira Hatanaka | 8e57b07 | 2018-10-01 21:51:28 +0000 | [diff] [blame] | 1749 | |
Akira Hatanaka | 8e57b07 | 2018-10-01 21:51:28 +0000 | [diff] [blame] | 1750 | markEscapingByrefs(*FunctionScopes.back(), *this); |
| 1751 | |
Richard Smith | 2fdd95c | 2019-05-31 00:45:09 +0000 | [diff] [blame] | 1752 | PoppedFunctionScopePtr Scope(FunctionScopes.pop_back_val(), |
| 1753 | PoppedFunctionScopeDeleter(this)); |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 1754 | |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 1755 | if (LangOpts.OpenMP) |
Richard Smith | 2fdd95c | 2019-05-31 00:45:09 +0000 | [diff] [blame] | 1756 | popOpenMPFunctionRegion(Scope.get()); |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 1757 | |
Ted Kremenek | 1767a27 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 1758 | // Issue any analysis-based warnings. |
| 1759 | if (WP && D) |
Richard Smith | 2fdd95c | 2019-05-31 00:45:09 +0000 | [diff] [blame] | 1760 | AnalysisWarnings.IssueWarnings(*WP, Scope.get(), D, BlockType); |
Aaron Ballman | 130a3b0 | 2014-05-15 20:58:55 +0000 | [diff] [blame] | 1761 | else |
| 1762 | for (const auto &PUD : Scope->PossiblyUnreachableDiags) |
| 1763 | Diag(PUD.Loc, PUD.PD); |
Ted Kremenek | 1767a27 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 1764 | |
Richard Smith | 2fdd95c | 2019-05-31 00:45:09 +0000 | [diff] [blame] | 1765 | return Scope; |
| 1766 | } |
| 1767 | |
| 1768 | void Sema::PoppedFunctionScopeDeleter:: |
| 1769 | operator()(sema::FunctionScopeInfo *Scope) const { |
| 1770 | // Stash the function scope for later reuse if it's for a normal function. |
| 1771 | if (Scope->isPlainFunction() && !Self->CachedFunctionScope) |
| 1772 | Self->CachedFunctionScope.reset(Scope); |
| 1773 | else |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 1774 | delete Scope; |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
Richard Smith | 6eb9b9e | 2018-02-03 00:44:57 +0000 | [diff] [blame] | 1777 | void Sema::PushCompoundScope(bool IsStmtExpr) { |
| 1778 | getCurFunction()->CompoundScopes.push_back(CompoundScopeInfo(IsStmtExpr)); |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | void Sema::PopCompoundScope() { |
| 1782 | FunctionScopeInfo *CurFunction = getCurFunction(); |
| 1783 | assert(!CurFunction->CompoundScopes.empty() && "mismatched push/pop"); |
| 1784 | |
| 1785 | CurFunction->CompoundScopes.pop_back(); |
| 1786 | } |
| 1787 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1788 | /// Determine whether any errors occurred within this function/method/ |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1789 | /// block. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1790 | bool Sema::hasAnyUnrecoverableErrorsInThisFunction() const { |
| 1791 | return getCurFunction()->ErrorTrap.hasUnrecoverableErrorOccurred(); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
Reid Kleckner | 87a3180 | 2018-03-12 21:43:02 +0000 | [diff] [blame] | 1794 | void Sema::setFunctionHasBranchIntoScope() { |
| 1795 | if (!FunctionScopes.empty()) |
| 1796 | FunctionScopes.back()->setHasBranchIntoScope(); |
| 1797 | } |
| 1798 | |
| 1799 | void Sema::setFunctionHasBranchProtectedScope() { |
| 1800 | if (!FunctionScopes.empty()) |
| 1801 | FunctionScopes.back()->setHasBranchProtectedScope(); |
| 1802 | } |
| 1803 | |
| 1804 | void Sema::setFunctionHasIndirectGoto() { |
| 1805 | if (!FunctionScopes.empty()) |
| 1806 | FunctionScopes.back()->setHasIndirectGoto(); |
| 1807 | } |
| 1808 | |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1809 | BlockScopeInfo *Sema::getCurBlock() { |
| 1810 | if (FunctionScopes.empty()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1811 | return nullptr; |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 1812 | |
Argyrios Kyrtzidis | ea75aad | 2014-04-26 18:29:13 +0000 | [diff] [blame] | 1813 | auto CurBSI = dyn_cast<BlockScopeInfo>(FunctionScopes.back()); |
| 1814 | if (CurBSI && CurBSI->TheDecl && |
| 1815 | !CurBSI->TheDecl->Encloses(CurContext)) { |
| 1816 | // We have switched contexts due to template instantiation. |
Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 1817 | assert(!CodeSynthesisContexts.empty()); |
Argyrios Kyrtzidis | ea75aad | 2014-04-26 18:29:13 +0000 | [diff] [blame] | 1818 | return nullptr; |
| 1819 | } |
| 1820 | |
| 1821 | return CurBSI; |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1822 | } |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 1823 | |
Reid Kleckner | 04f9bca | 2018-03-07 22:48:35 +0000 | [diff] [blame] | 1824 | FunctionScopeInfo *Sema::getEnclosingFunction() const { |
| 1825 | if (FunctionScopes.empty()) |
| 1826 | return nullptr; |
| 1827 | |
| 1828 | for (int e = FunctionScopes.size() - 1; e >= 0; --e) { |
| 1829 | if (isa<sema::BlockScopeInfo>(FunctionScopes[e])) |
| 1830 | continue; |
| 1831 | return FunctionScopes[e]; |
| 1832 | } |
| 1833 | return nullptr; |
| 1834 | } |
| 1835 | |
Richard Smith | b26bc34 | 2019-08-26 22:51:28 +0000 | [diff] [blame] | 1836 | LambdaScopeInfo *Sema::getEnclosingLambda() const { |
| 1837 | for (auto *Scope : llvm::reverse(FunctionScopes)) { |
| 1838 | if (auto *LSI = dyn_cast<sema::LambdaScopeInfo>(Scope)) { |
| 1839 | if (LSI->Lambda && !LSI->Lambda->Encloses(CurContext)) { |
| 1840 | // We have switched contexts due to template instantiation. |
| 1841 | // FIXME: We should swap out the FunctionScopes during code synthesis |
| 1842 | // so that we don't need to check for this. |
| 1843 | assert(!CodeSynthesisContexts.empty()); |
| 1844 | return nullptr; |
| 1845 | } |
| 1846 | return LSI; |
| 1847 | } |
| 1848 | } |
| 1849 | return nullptr; |
| 1850 | } |
| 1851 | |
Akira Hatanaka | 7cbbb88 | 2017-03-01 06:11:25 +0000 | [diff] [blame] | 1852 | LambdaScopeInfo *Sema::getCurLambda(bool IgnoreNonLambdaCapturingScope) { |
Eli Friedman | 4817cf7 | 2012-01-06 03:05:34 +0000 | [diff] [blame] | 1853 | if (FunctionScopes.empty()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1854 | return nullptr; |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 1855 | |
Alexey Bataev | 31939e3 | 2016-11-11 12:36:20 +0000 | [diff] [blame] | 1856 | auto I = FunctionScopes.rbegin(); |
Akira Hatanaka | 7cbbb88 | 2017-03-01 06:11:25 +0000 | [diff] [blame] | 1857 | if (IgnoreNonLambdaCapturingScope) { |
Alexey Bataev | 31939e3 | 2016-11-11 12:36:20 +0000 | [diff] [blame] | 1858 | auto E = FunctionScopes.rend(); |
Akira Hatanaka | 7cbbb88 | 2017-03-01 06:11:25 +0000 | [diff] [blame] | 1859 | while (I != E && isa<CapturingScopeInfo>(*I) && !isa<LambdaScopeInfo>(*I)) |
Alexey Bataev | 31939e3 | 2016-11-11 12:36:20 +0000 | [diff] [blame] | 1860 | ++I; |
| 1861 | if (I == E) |
| 1862 | return nullptr; |
| 1863 | } |
| 1864 | auto *CurLSI = dyn_cast<LambdaScopeInfo>(*I); |
Argyrios Kyrtzidis | ea75aad | 2014-04-26 18:29:13 +0000 | [diff] [blame] | 1865 | if (CurLSI && CurLSI->Lambda && |
| 1866 | !CurLSI->Lambda->Encloses(CurContext)) { |
| 1867 | // We have switched contexts due to template instantiation. |
Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 1868 | assert(!CodeSynthesisContexts.empty()); |
Argyrios Kyrtzidis | ea75aad | 2014-04-26 18:29:13 +0000 | [diff] [blame] | 1869 | return nullptr; |
| 1870 | } |
| 1871 | |
| 1872 | return CurLSI; |
Eli Friedman | 4817cf7 | 2012-01-06 03:05:34 +0000 | [diff] [blame] | 1873 | } |
Richard Smith | b26bc34 | 2019-08-26 22:51:28 +0000 | [diff] [blame] | 1874 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1875 | // We have a generic lambda if we parsed auto parameters, or we have |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 1876 | // an associated template parameter list. |
| 1877 | LambdaScopeInfo *Sema::getCurGenericLambda() { |
| 1878 | if (LambdaScopeInfo *LSI = getCurLambda()) { |
Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 1879 | return (LSI->TemplateParams.size() || |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1880 | LSI->GLTemplateParameterList) ? LSI : nullptr; |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 1881 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1882 | return nullptr; |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 1883 | } |
| 1884 | |
Eli Friedman | 4817cf7 | 2012-01-06 03:05:34 +0000 | [diff] [blame] | 1885 | |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 1886 | void Sema::ActOnComment(SourceRange Comment) { |
Ted Kremenek | b47e6bc | 2012-09-13 06:41:18 +0000 | [diff] [blame] | 1887 | if (!LangOpts.RetainCommentsFromSystemHeaders && |
| 1888 | SourceMgr.isInSystemHeader(Comment.getBegin())) |
| 1889 | return; |
David L. Jones | 13d5a87 | 2018-03-02 00:07:45 +0000 | [diff] [blame] | 1890 | RawComment RC(SourceMgr, Comment, LangOpts.CommentOpts, false); |
Dmitri Gribenko | 052f60d | 2012-06-22 16:02:55 +0000 | [diff] [blame] | 1891 | if (RC.isAlmostTrailingComment()) { |
| 1892 | SourceRange MagicMarkerRange(Comment.getBegin(), |
| 1893 | Comment.getBegin().getLocWithOffset(3)); |
| 1894 | StringRef MagicMarkerText; |
| 1895 | switch (RC.getKind()) { |
Abramo Bagnara | e06a888 | 2012-07-04 07:30:26 +0000 | [diff] [blame] | 1896 | case RawComment::RCK_OrdinaryBCPL: |
Dmitri Gribenko | 052f60d | 2012-06-22 16:02:55 +0000 | [diff] [blame] | 1897 | MagicMarkerText = "///<"; |
| 1898 | break; |
Abramo Bagnara | e06a888 | 2012-07-04 07:30:26 +0000 | [diff] [blame] | 1899 | case RawComment::RCK_OrdinaryC: |
Dmitri Gribenko | 052f60d | 2012-06-22 16:02:55 +0000 | [diff] [blame] | 1900 | MagicMarkerText = "/**<"; |
| 1901 | break; |
| 1902 | default: |
| 1903 | llvm_unreachable("if this is an almost Doxygen comment, " |
| 1904 | "it should be ordinary"); |
| 1905 | } |
| 1906 | Diag(Comment.getBegin(), diag::warn_not_a_doxygen_trailing_member_comment) << |
| 1907 | FixItHint::CreateReplacement(MagicMarkerRange, MagicMarkerText); |
| 1908 | } |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 1909 | Context.addComment(RC); |
| 1910 | } |
| 1911 | |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 1912 | // Pin this vtable to this file. |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 1913 | ExternalSemaSource::~ExternalSemaSource() {} |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1914 | |
Douglas Gregor | e171601 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 1915 | void ExternalSemaSource::ReadMethodPool(Selector Sel) { } |
Manman Ren | a0f31a0 | 2016-04-29 19:04:05 +0000 | [diff] [blame] | 1916 | void ExternalSemaSource::updateOutOfDateSelector(Selector Sel) { } |
Sebastian Redl | c1ca90a | 2010-09-28 20:23:00 +0000 | [diff] [blame] | 1917 | |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1918 | void ExternalSemaSource::ReadKnownNamespaces( |
Michael Gottesman | bf0fd39 | 2013-01-20 01:04:14 +0000 | [diff] [blame] | 1919 | SmallVectorImpl<NamespaceDecl *> &Namespaces) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
Nick Lewycky | 9c7eb1d | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 1922 | void ExternalSemaSource::ReadUndefinedButUsed( |
Richard Smith | d6a04d7 | 2016-03-25 21:49:43 +0000 | [diff] [blame] | 1923 | llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {} |
Nick Lewycky | 8334af8 | 2013-01-26 00:35:08 +0000 | [diff] [blame] | 1924 | |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 1925 | void ExternalSemaSource::ReadMismatchingDeleteExpressions(llvm::MapVector< |
| 1926 | FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &) {} |
| 1927 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1928 | /// Figure out if an expression could be turned into a call. |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 1929 | /// |
| 1930 | /// Use this when trying to recover from an error where the programmer may have |
| 1931 | /// written just the name of a function instead of actually calling it. |
| 1932 | /// |
| 1933 | /// \param E - The expression to examine. |
| 1934 | /// \param ZeroArgCallReturnTy - If the expression can be turned into a call |
| 1935 | /// with no arguments, this parameter is set to the type returned by such a |
| 1936 | /// call; otherwise, it is set to an empty QualType. |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 1937 | /// \param OverloadSet - If the expression is an overloaded function |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 1938 | /// name, this parameter is populated with the decls of the various overloads. |
David Blaikie | e5323aa | 2013-06-21 23:54:45 +0000 | [diff] [blame] | 1939 | bool Sema::tryExprAsCall(Expr &E, QualType &ZeroArgCallReturnTy, |
| 1940 | UnresolvedSetImpl &OverloadSet) { |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 1941 | ZeroArgCallReturnTy = QualType(); |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 1942 | OverloadSet.clear(); |
| 1943 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1944 | const OverloadExpr *Overloads = nullptr; |
David Blaikie | e5323aa | 2013-06-21 23:54:45 +0000 | [diff] [blame] | 1945 | bool IsMemExpr = false; |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 1946 | if (E.getType() == Context.OverloadTy) { |
| 1947 | OverloadExpr::FindResult FR = OverloadExpr::find(const_cast<Expr*>(&E)); |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 1948 | |
David Blaikie | 6df859d8 | 2013-06-04 00:28:46 +0000 | [diff] [blame] | 1949 | // Ignore overloads that are pointer-to-member constants. |
| 1950 | if (FR.HasFormOfMemberPointer) |
| 1951 | return false; |
| 1952 | |
| 1953 | Overloads = FR.Expression; |
| 1954 | } else if (E.getType() == Context.BoundMemberTy) { |
| 1955 | Overloads = dyn_cast<UnresolvedMemberExpr>(E.IgnoreParens()); |
David Blaikie | e5323aa | 2013-06-21 23:54:45 +0000 | [diff] [blame] | 1956 | IsMemExpr = true; |
David Blaikie | 6df859d8 | 2013-06-04 00:28:46 +0000 | [diff] [blame] | 1957 | } |
David Blaikie | e5323aa | 2013-06-21 23:54:45 +0000 | [diff] [blame] | 1958 | |
| 1959 | bool Ambiguous = false; |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 1960 | bool IsMV = false; |
David Blaikie | e5323aa | 2013-06-21 23:54:45 +0000 | [diff] [blame] | 1961 | |
David Blaikie | 6df859d8 | 2013-06-04 00:28:46 +0000 | [diff] [blame] | 1962 | if (Overloads) { |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 1963 | for (OverloadExpr::decls_iterator it = Overloads->decls_begin(), |
| 1964 | DeclsEnd = Overloads->decls_end(); it != DeclsEnd; ++it) { |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 1965 | OverloadSet.addDecl(*it); |
| 1966 | |
David Blaikie | e5323aa | 2013-06-21 23:54:45 +0000 | [diff] [blame] | 1967 | // Check whether the function is a non-template, non-member which takes no |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 1968 | // arguments. |
David Blaikie | e5323aa | 2013-06-21 23:54:45 +0000 | [diff] [blame] | 1969 | if (IsMemExpr) |
| 1970 | continue; |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 1971 | if (const FunctionDecl *OverloadDecl |
| 1972 | = dyn_cast<FunctionDecl>((*it)->getUnderlyingDecl())) { |
David Blaikie | 6df859d8 | 2013-06-04 00:28:46 +0000 | [diff] [blame] | 1973 | if (OverloadDecl->getMinRequiredArguments() == 0) { |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 1974 | if (!ZeroArgCallReturnTy.isNull() && !Ambiguous && |
| 1975 | (!IsMV || !(OverloadDecl->isCPUDispatchMultiVersion() || |
| 1976 | OverloadDecl->isCPUSpecificMultiVersion()))) { |
David Blaikie | 6df859d8 | 2013-06-04 00:28:46 +0000 | [diff] [blame] | 1977 | ZeroArgCallReturnTy = QualType(); |
| 1978 | Ambiguous = true; |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 1979 | } else { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1980 | ZeroArgCallReturnTy = OverloadDecl->getReturnType(); |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 1981 | IsMV = OverloadDecl->isCPUDispatchMultiVersion() || |
| 1982 | OverloadDecl->isCPUSpecificMultiVersion(); |
| 1983 | } |
David Blaikie | 6df859d8 | 2013-06-04 00:28:46 +0000 | [diff] [blame] | 1984 | } |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 1985 | } |
| 1986 | } |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 1987 | |
David Blaikie | e5323aa | 2013-06-21 23:54:45 +0000 | [diff] [blame] | 1988 | // If it's not a member, use better machinery to try to resolve the call |
| 1989 | if (!IsMemExpr) |
| 1990 | return !ZeroArgCallReturnTy.isNull(); |
| 1991 | } |
| 1992 | |
| 1993 | // Attempt to call the member with no arguments - this will correctly handle |
| 1994 | // member templates with defaults/deduction of template arguments, overloads |
| 1995 | // with default arguments, etc. |
Eli Friedman | 544c956 | 2013-07-08 23:35:04 +0000 | [diff] [blame] | 1996 | if (IsMemExpr && !E.isTypeDependent()) { |
Richard Smith | 2e3ed4a | 2019-08-16 19:53:22 +0000 | [diff] [blame] | 1997 | Sema::TentativeAnalysisScope Trap(*this); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1998 | ExprResult R = BuildCallToMemberFunction(nullptr, &E, SourceLocation(), |
| 1999 | None, SourceLocation()); |
David Blaikie | e5323aa | 2013-06-21 23:54:45 +0000 | [diff] [blame] | 2000 | if (R.isUsable()) { |
| 2001 | ZeroArgCallReturnTy = R.get()->getType(); |
| 2002 | return true; |
| 2003 | } |
| 2004 | return false; |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 2007 | if (const DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E.IgnoreParens())) { |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 2008 | if (const FunctionDecl *Fun = dyn_cast<FunctionDecl>(DeclRef->getDecl())) { |
| 2009 | if (Fun->getMinRequiredArguments() == 0) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2010 | ZeroArgCallReturnTy = Fun->getReturnType(); |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 2011 | return true; |
| 2012 | } |
| 2013 | } |
| 2014 | |
| 2015 | // We don't have an expression that's convenient to get a FunctionDecl from, |
| 2016 | // but we can at least check if the type is "function of 0 arguments". |
| 2017 | QualType ExprTy = E.getType(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2018 | const FunctionType *FunTy = nullptr; |
Matt Beaumont-Gay | 330a5b4 | 2011-05-05 00:59:35 +0000 | [diff] [blame] | 2019 | QualType PointeeTy = ExprTy->getPointeeType(); |
| 2020 | if (!PointeeTy.isNull()) |
| 2021 | FunTy = PointeeTy->getAs<FunctionType>(); |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 2022 | if (!FunTy) |
| 2023 | FunTy = ExprTy->getAs<FunctionType>(); |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 2024 | |
| 2025 | if (const FunctionProtoType *FPT = |
| 2026 | dyn_cast_or_null<FunctionProtoType>(FunTy)) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2027 | if (FPT->getNumParams() == 0) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2028 | ZeroArgCallReturnTy = FunTy->getReturnType(); |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 2029 | return true; |
| 2030 | } |
| 2031 | return false; |
| 2032 | } |
| 2033 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2034 | /// Give notes for a set of overloads. |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 2035 | /// |
David Blaikie | e5323aa | 2013-06-21 23:54:45 +0000 | [diff] [blame] | 2036 | /// A companion to tryExprAsCall. In cases when the name that the programmer |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 2037 | /// wrote was an overloaded function, we may be able to make some guesses about |
| 2038 | /// plausible overloads based on their return types; such guesses can be handed |
| 2039 | /// off to this method to be emitted as notes. |
| 2040 | /// |
| 2041 | /// \param Overloads - The overloads to note. |
| 2042 | /// \param FinalNoteLoc - If we've suppressed printing some overloads due to |
| 2043 | /// -fshow-overloads=best, this is the location to attach to the note about too |
| 2044 | /// many candidates. Typically this will be the location of the original |
| 2045 | /// ill-formed expression. |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 2046 | static void noteOverloads(Sema &S, const UnresolvedSetImpl &Overloads, |
| 2047 | const SourceLocation FinalNoteLoc) { |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 2048 | int ShownOverloads = 0; |
| 2049 | int SuppressedOverloads = 0; |
| 2050 | for (UnresolvedSetImpl::iterator It = Overloads.begin(), |
| 2051 | DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) { |
| 2052 | // FIXME: Magic number for max shown overloads stolen from |
| 2053 | // OverloadCandidateSet::NoteCandidates. |
Douglas Gregor | 7959178 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 2054 | if (ShownOverloads >= 4 && S.Diags.getShowOverloads() == Ovl_Best) { |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 2055 | ++SuppressedOverloads; |
| 2056 | continue; |
| 2057 | } |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 2058 | |
| 2059 | NamedDecl *Fn = (*It)->getUnderlyingDecl(); |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 2060 | // Don't print overloads for non-default multiversioned functions. |
| 2061 | if (const auto *FD = Fn->getAsFunction()) { |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 2062 | if (FD->isMultiVersion() && FD->hasAttr<TargetAttr>() && |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 2063 | !FD->getAttr<TargetAttr>()->isDefaultVersion()) |
| 2064 | continue; |
| 2065 | } |
Abramo Bagnara | dc1646d | 2011-11-15 21:43:28 +0000 | [diff] [blame] | 2066 | S.Diag(Fn->getLocation(), diag::note_possible_target_of_call); |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 2067 | ++ShownOverloads; |
| 2068 | } |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 2069 | |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 2070 | if (SuppressedOverloads) |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 2071 | S.Diag(FinalNoteLoc, diag::note_ovl_too_many_candidates) |
| 2072 | << SuppressedOverloads; |
| 2073 | } |
| 2074 | |
| 2075 | static void notePlausibleOverloads(Sema &S, SourceLocation Loc, |
| 2076 | const UnresolvedSetImpl &Overloads, |
| 2077 | bool (*IsPlausibleResult)(QualType)) { |
| 2078 | if (!IsPlausibleResult) |
| 2079 | return noteOverloads(S, Overloads, Loc); |
| 2080 | |
| 2081 | UnresolvedSet<2> PlausibleOverloads; |
| 2082 | for (OverloadExpr::decls_iterator It = Overloads.begin(), |
| 2083 | DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) { |
| 2084 | const FunctionDecl *OverloadDecl = cast<FunctionDecl>(*It); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2085 | QualType OverloadResultTy = OverloadDecl->getReturnType(); |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 2086 | if (IsPlausibleResult(OverloadResultTy)) |
| 2087 | PlausibleOverloads.addDecl(It.getDecl()); |
| 2088 | } |
| 2089 | noteOverloads(S, PlausibleOverloads, Loc); |
| 2090 | } |
| 2091 | |
| 2092 | /// Determine whether the given expression can be called by just |
| 2093 | /// putting parentheses after it. Notably, expressions with unary |
| 2094 | /// operators can't be because the unary operator will start parsing |
| 2095 | /// outside the call. |
| 2096 | static bool IsCallableWithAppend(Expr *E) { |
| 2097 | E = E->IgnoreImplicit(); |
| 2098 | return (!isa<CStyleCastExpr>(E) && |
| 2099 | !isa<UnaryOperator>(E) && |
| 2100 | !isa<BinaryOperator>(E) && |
| 2101 | !isa<CXXOperatorCallExpr>(E)); |
| 2102 | } |
| 2103 | |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 2104 | static bool IsCPUDispatchCPUSpecificMultiVersion(const Expr *E) { |
| 2105 | if (const auto *UO = dyn_cast<UnaryOperator>(E)) |
| 2106 | E = UO->getSubExpr(); |
| 2107 | |
| 2108 | if (const auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 2109 | if (ULE->getNumDecls() == 0) |
| 2110 | return false; |
| 2111 | |
| 2112 | const NamedDecl *ND = *ULE->decls_begin(); |
| 2113 | if (const auto *FD = dyn_cast<FunctionDecl>(ND)) |
| 2114 | return FD->isCPUDispatchMultiVersion() || FD->isCPUSpecificMultiVersion(); |
| 2115 | } |
| 2116 | return false; |
| 2117 | } |
| 2118 | |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 2119 | bool Sema::tryToRecoverWithCall(ExprResult &E, const PartialDiagnostic &PD, |
| 2120 | bool ForceComplain, |
| 2121 | bool (*IsPlausibleResult)(QualType)) { |
| 2122 | SourceLocation Loc = E.get()->getExprLoc(); |
| 2123 | SourceRange Range = E.get()->getSourceRange(); |
| 2124 | |
| 2125 | QualType ZeroArgCallTy; |
| 2126 | UnresolvedSet<4> Overloads; |
David Blaikie | e5323aa | 2013-06-21 23:54:45 +0000 | [diff] [blame] | 2127 | if (tryExprAsCall(*E.get(), ZeroArgCallTy, Overloads) && |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 2128 | !ZeroArgCallTy.isNull() && |
| 2129 | (!IsPlausibleResult || IsPlausibleResult(ZeroArgCallTy))) { |
| 2130 | // At this point, we know E is potentially callable with 0 |
| 2131 | // arguments and that it returns something of a reasonable type, |
| 2132 | // so we can emit a fixit and carry on pretending that E was |
| 2133 | // actually a CallExpr. |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 2134 | SourceLocation ParenInsertionLoc = getLocForEndOfToken(Range.getEnd()); |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 2135 | bool IsMV = IsCPUDispatchCPUSpecificMultiVersion(E.get()); |
| 2136 | Diag(Loc, PD) << /*zero-arg*/ 1 << IsMV << Range |
| 2137 | << (IsCallableWithAppend(E.get()) |
| 2138 | ? FixItHint::CreateInsertion(ParenInsertionLoc, "()") |
| 2139 | : FixItHint()); |
| 2140 | if (!IsMV) |
| 2141 | notePlausibleOverloads(*this, Loc, Overloads, IsPlausibleResult); |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 2142 | |
| 2143 | // FIXME: Try this before emitting the fixit, and suppress diagnostics |
| 2144 | // while doing so. |
Richard Smith | 255b85f | 2019-05-08 01:36:36 +0000 | [diff] [blame] | 2145 | E = BuildCallExpr(nullptr, E.get(), Range.getEnd(), None, |
Nick Lewycky | 1e43d95 | 2013-08-21 19:09:44 +0000 | [diff] [blame] | 2146 | Range.getEnd().getLocWithOffset(1)); |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 2147 | return true; |
| 2148 | } |
| 2149 | |
| 2150 | if (!ForceComplain) return false; |
| 2151 | |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 2152 | bool IsMV = IsCPUDispatchCPUSpecificMultiVersion(E.get()); |
| 2153 | Diag(Loc, PD) << /*not zero-arg*/ 0 << IsMV << Range; |
| 2154 | if (!IsMV) |
| 2155 | notePlausibleOverloads(*this, Loc, Overloads, IsPlausibleResult); |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 2156 | E = ExprError(); |
| 2157 | return true; |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 2158 | } |
Argyrios Kyrtzidis | 3e56dd4 | 2013-03-14 22:56:43 +0000 | [diff] [blame] | 2159 | |
| 2160 | IdentifierInfo *Sema::getSuperIdentifier() const { |
| 2161 | if (!Ident_super) |
| 2162 | Ident_super = &Context.Idents.get("super"); |
| 2163 | return Ident_super; |
| 2164 | } |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 2165 | |
Nico Weber | e1687c5 | 2013-06-20 21:44:55 +0000 | [diff] [blame] | 2166 | IdentifierInfo *Sema::getFloat128Identifier() const { |
| 2167 | if (!Ident___float128) |
| 2168 | Ident___float128 = &Context.Idents.get("__float128"); |
| 2169 | return Ident___float128; |
| 2170 | } |
| 2171 | |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 2172 | void Sema::PushCapturedRegionScope(Scope *S, CapturedDecl *CD, RecordDecl *RD, |
Joel E. Denny | 7d5bc55 | 2019-08-22 03:34:30 +0000 | [diff] [blame] | 2173 | CapturedRegionKind K, |
| 2174 | unsigned OpenMPCaptureLevel) { |
| 2175 | auto *CSI = new CapturedRegionScopeInfo( |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 2176 | getDiagnostics(), S, CD, RD, CD->getContextParam(), K, |
Joel E. Denny | 7d5bc55 | 2019-08-22 03:34:30 +0000 | [diff] [blame] | 2177 | (getLangOpts().OpenMP && K == CR_OpenMP) ? getOpenMPNestingLevel() : 0, |
| 2178 | OpenMPCaptureLevel); |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 2179 | CSI->ReturnType = Context.VoidTy; |
| 2180 | FunctionScopes.push_back(CSI); |
| 2181 | } |
| 2182 | |
| 2183 | CapturedRegionScopeInfo *Sema::getCurCapturedRegion() { |
| 2184 | if (FunctionScopes.empty()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2185 | return nullptr; |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 2186 | |
| 2187 | return dyn_cast<CapturedRegionScopeInfo>(FunctionScopes.back()); |
| 2188 | } |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2189 | |
| 2190 | const llvm::MapVector<FieldDecl *, Sema::DeleteLocs> & |
| 2191 | Sema::getMismatchingDeleteExpressions() const { |
| 2192 | return DeleteExprs; |
| 2193 | } |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 2194 | |
| 2195 | void Sema::setOpenCLExtensionForType(QualType T, llvm::StringRef ExtStr) { |
| 2196 | if (ExtStr.empty()) |
| 2197 | return; |
| 2198 | llvm::SmallVector<StringRef, 1> Exts; |
| 2199 | ExtStr.split(Exts, " ", /* limit */ -1, /* keep empty */ false); |
| 2200 | auto CanT = T.getCanonicalType().getTypePtr(); |
| 2201 | for (auto &I : Exts) |
| 2202 | OpenCLTypeExtMap[CanT].insert(I.str()); |
| 2203 | } |
| 2204 | |
| 2205 | void Sema::setOpenCLExtensionForDecl(Decl *FD, StringRef ExtStr) { |
| 2206 | llvm::SmallVector<StringRef, 1> Exts; |
| 2207 | ExtStr.split(Exts, " ", /* limit */ -1, /* keep empty */ false); |
| 2208 | if (Exts.empty()) |
| 2209 | return; |
| 2210 | for (auto &I : Exts) |
| 2211 | OpenCLDeclExtMap[FD].insert(I.str()); |
| 2212 | } |
| 2213 | |
| 2214 | void Sema::setCurrentOpenCLExtensionForType(QualType T) { |
| 2215 | if (CurrOpenCLExtension.empty()) |
| 2216 | return; |
| 2217 | setOpenCLExtensionForType(T, CurrOpenCLExtension); |
| 2218 | } |
| 2219 | |
| 2220 | void Sema::setCurrentOpenCLExtensionForDecl(Decl *D) { |
| 2221 | if (CurrOpenCLExtension.empty()) |
| 2222 | return; |
| 2223 | setOpenCLExtensionForDecl(D, CurrOpenCLExtension); |
| 2224 | } |
| 2225 | |
Andrew Savonichev | 16f1699 | 2018-10-11 13:35:34 +0000 | [diff] [blame] | 2226 | std::string Sema::getOpenCLExtensionsFromDeclExtMap(FunctionDecl *FD) { |
| 2227 | if (!OpenCLDeclExtMap.empty()) |
| 2228 | return getOpenCLExtensionsFromExtMap(FD, OpenCLDeclExtMap); |
| 2229 | |
| 2230 | return ""; |
| 2231 | } |
| 2232 | |
| 2233 | std::string Sema::getOpenCLExtensionsFromTypeExtMap(FunctionType *FT) { |
| 2234 | if (!OpenCLTypeExtMap.empty()) |
| 2235 | return getOpenCLExtensionsFromExtMap(FT, OpenCLTypeExtMap); |
| 2236 | |
| 2237 | return ""; |
| 2238 | } |
| 2239 | |
| 2240 | template <typename T, typename MapT> |
| 2241 | std::string Sema::getOpenCLExtensionsFromExtMap(T *FDT, MapT &Map) { |
| 2242 | std::string ExtensionNames = ""; |
| 2243 | auto Loc = Map.find(FDT); |
| 2244 | |
| 2245 | for (auto const& I : Loc->second) { |
| 2246 | ExtensionNames += I; |
| 2247 | ExtensionNames += " "; |
| 2248 | } |
| 2249 | ExtensionNames.pop_back(); |
| 2250 | |
| 2251 | return ExtensionNames; |
| 2252 | } |
| 2253 | |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 2254 | bool Sema::isOpenCLDisabledDecl(Decl *FD) { |
| 2255 | auto Loc = OpenCLDeclExtMap.find(FD); |
| 2256 | if (Loc == OpenCLDeclExtMap.end()) |
| 2257 | return false; |
| 2258 | for (auto &I : Loc->second) { |
| 2259 | if (!getOpenCLOptions().isEnabled(I)) |
| 2260 | return true; |
| 2261 | } |
| 2262 | return false; |
| 2263 | } |
| 2264 | |
| 2265 | template <typename T, typename DiagLocT, typename DiagInfoT, typename MapT> |
| 2266 | bool Sema::checkOpenCLDisabledTypeOrDecl(T D, DiagLocT DiagLoc, |
| 2267 | DiagInfoT DiagInfo, MapT &Map, |
| 2268 | unsigned Selector, |
| 2269 | SourceRange SrcRange) { |
| 2270 | auto Loc = Map.find(D); |
| 2271 | if (Loc == Map.end()) |
| 2272 | return false; |
| 2273 | bool Disabled = false; |
| 2274 | for (auto &I : Loc->second) { |
| 2275 | if (I != CurrOpenCLExtension && !getOpenCLOptions().isEnabled(I)) { |
| 2276 | Diag(DiagLoc, diag::err_opencl_requires_extension) << Selector << DiagInfo |
| 2277 | << I << SrcRange; |
| 2278 | Disabled = true; |
| 2279 | } |
| 2280 | } |
| 2281 | return Disabled; |
| 2282 | } |
| 2283 | |
| 2284 | bool Sema::checkOpenCLDisabledTypeDeclSpec(const DeclSpec &DS, QualType QT) { |
| 2285 | // Check extensions for declared types. |
| 2286 | Decl *Decl = nullptr; |
| 2287 | if (auto TypedefT = dyn_cast<TypedefType>(QT.getTypePtr())) |
| 2288 | Decl = TypedefT->getDecl(); |
| 2289 | if (auto TagT = dyn_cast<TagType>(QT.getCanonicalType().getTypePtr())) |
| 2290 | Decl = TagT->getDecl(); |
| 2291 | auto Loc = DS.getTypeSpecTypeLoc(); |
Alexey Sotkin | 73ae7cb | 2018-09-03 11:43:22 +0000 | [diff] [blame] | 2292 | |
| 2293 | // Check extensions for vector types. |
| 2294 | // e.g. double4 is not allowed when cl_khr_fp64 is absent. |
| 2295 | if (QT->isExtVectorType()) { |
| 2296 | auto TypePtr = QT->castAs<ExtVectorType>()->getElementType().getTypePtr(); |
| 2297 | return checkOpenCLDisabledTypeOrDecl(TypePtr, Loc, QT, OpenCLTypeExtMap); |
| 2298 | } |
| 2299 | |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 2300 | if (checkOpenCLDisabledTypeOrDecl(Decl, Loc, QT, OpenCLDeclExtMap)) |
| 2301 | return true; |
| 2302 | |
| 2303 | // Check extensions for builtin types. |
| 2304 | return checkOpenCLDisabledTypeOrDecl(QT.getCanonicalType().getTypePtr(), Loc, |
| 2305 | QT, OpenCLTypeExtMap); |
| 2306 | } |
| 2307 | |
Joey Gouly | 186791d | 2017-06-30 14:23:01 +0000 | [diff] [blame] | 2308 | bool Sema::checkOpenCLDisabledDecl(const NamedDecl &D, const Expr &E) { |
| 2309 | IdentifierInfo *FnName = D.getIdentifier(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2310 | return checkOpenCLDisabledTypeOrDecl(&D, E.getBeginLoc(), FnName, |
Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 2311 | OpenCLDeclExtMap, 1, D.getSourceRange()); |
| 2312 | } |