@@ -141,90 +141,28 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult &Result) {
141141 if (!FileRange.isValid ())
142142 return ;
143143
144- // FIXME: Instead of re-lexing and looking for specific macros such as
145- // 'ABSTRACT', properly store the location of 'virtual' and '= 0' in each
146- // FunctionDecl.
144+ // FIXME: Instead of re-lexing and looking for the 'virtual' token,
145+ // store the location of 'virtual' in each FunctionDecl.
147146 SmallVector<Token, 16 > Tokens = parseTokens (FileRange, Result);
148147
149148 // Add 'override' on inline declarations that don't already have it.
150149 if (!HasFinal && !HasOverride) {
151- SourceLocation InsertLoc;
152- std::string ReplacementText = (OverrideSpelling + " " ).str ();
153- const SourceLocation MethodLoc = Method->getLocation ();
154-
155- for (const Token T : Tokens) {
156- if (T.is (tok::kw___attribute) &&
157- !Sources.isBeforeInTranslationUnit (T.getLocation (), MethodLoc)) {
158- InsertLoc = T.getLocation ();
159- break ;
160- }
161- }
162-
163- if (Method->hasAttrs ()) {
164- for (const clang::Attr *A : Method->getAttrs ()) {
165- if (!A->isImplicit () && !A->isInherited ()) {
166- const SourceLocation Loc =
167- Sources.getExpansionLoc (A->getRange ().getBegin ());
168- if ((!InsertLoc.isValid () ||
169- Sources.isBeforeInTranslationUnit (Loc, InsertLoc)) &&
170- !Sources.isBeforeInTranslationUnit (Loc, MethodLoc))
171- InsertLoc = Loc;
172- }
173- }
174- }
175-
176- if (InsertLoc.isInvalid () && Method->doesThisDeclarationHaveABody () &&
177- Method->getBody () && !Method->isDefaulted ()) {
178- // For methods with inline definition, add the override keyword at the
179- // end of the declaration of the function, but prefer to put it on the
180- // same line as the declaration if the beginning brace for the start of
181- // the body falls on the next line.
182- ReplacementText = (" " + OverrideSpelling).str ();
183- auto *LastTokenIter = std::prev (Tokens.end ());
184- // When try statement is used instead of compound statement as
185- // method body - insert override keyword before it.
186- if (LastTokenIter->is (tok::kw_try))
187- LastTokenIter = std::prev (LastTokenIter);
188- InsertLoc = LastTokenIter->getEndLoc ();
189- }
190-
191- if (!InsertLoc.isValid ()) {
192- // For declarations marked with "= 0" or "= [default|delete]", the end
193- // location will point until after those markings. Therefore, the override
194- // keyword shouldn't be inserted at the end, but before the '='.
195- if (Tokens.size () > 2 &&
196- (getText (Tokens.back (), Sources) == " 0" ||
197- Tokens.back ().is (tok::kw_default) ||
198- Tokens.back ().is (tok::kw_delete)) &&
199- getText (Tokens[Tokens.size () - 2 ], Sources) == " =" ) {
200- InsertLoc = Tokens[Tokens.size () - 2 ].getLocation ();
201- // Check if we need to insert a space.
202- if ((Tokens[Tokens.size () - 2 ].getFlags () & Token::LeadingSpace) == 0 )
203- ReplacementText = (" " + OverrideSpelling + " " ).str ();
204- } else if (getText (Tokens.back (), Sources) == " ABSTRACT" )
205- InsertLoc = Tokens.back ().getLocation ();
206- }
207-
208- if (!InsertLoc.isValid ()) {
209- InsertLoc = FileRange.getEnd ();
210- ReplacementText = (" " + OverrideSpelling).str ();
211- }
212-
213150 // If the override macro has been specified just ensure it exists,
214151 // if not don't apply a fixit but keep the warning.
215152 if (OverrideSpelling != " override" &&
216153 !Context.Idents .get (OverrideSpelling).hasMacroDefinition ())
217154 return ;
218155
219- Diag << FixItHint::CreateInsertion (InsertLoc, ReplacementText);
156+ Diag << FixItHint::CreateInsertion (
157+ Lexer::getLocForEndOfToken (
158+ Method->getTypeSourceInfo ()->getTypeLoc ().getEndLoc (), 0 , Sources,
159+ getLangOpts ()),
160+ (" " + OverrideSpelling).str ());
220161 }
221162
222- if (HasFinal && HasOverride && !AllowOverrideAndFinal) {
223- const SourceLocation OverrideLoc =
224- Method->getAttr <OverrideAttr>()->getLocation ();
163+ if (HasFinal && HasOverride && !AllowOverrideAndFinal)
225164 Diag << FixItHint::CreateRemoval (
226- CharSourceRange::getTokenRange (OverrideLoc, OverrideLoc));
227- }
165+ Method->getAttr <OverrideAttr>()->getLocation ());
228166
229167 if (HasVirtual) {
230168 for (const Token Tok : Tokens) {
0 commit comments