Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions clang/lib/Tooling/Refactor/FillInEnumSwitchCases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,12 @@ clang::tooling::initiateFillInEnumSwitchCasesOperation(

if (!ED)
return RefactoringOperationResult("The switch doesn't operate on an enum");
if (!ED->isCompleteDefinition())
return RefactoringOperationResult("The enum type is incomplete");

if (!ED->isCompleteDefinition()) {
ED = ED->getDefinition();
if (!ED)
return RefactoringOperationResult("The enum type is incomplete");
}

if (Switch->isAllEnumCasesCovered())
return RefactoringOperationResult("All enum cases are already covered");
Expand Down
48 changes: 23 additions & 25 deletions clang/lib/Tooling/Refactor/ImplementDeclaredMethods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,35 +262,42 @@ ImplementDeclaredCXXMethodsOperation::runInImplementationAST(
std::string MethodString;
llvm::raw_string_ostream OS(MethodString);

PrintingPolicy PP = Context.getPrintingPolicy();
PP.PolishForDeclaration = true;
PP.SupressStorageClassSpecifiers = true;
PP.SuppressStrongLifetime = true;
PP.SuppressLifetimeQualifiers = true;
PP.SuppressUnwrittenScope = true;

// Callback class for skipping namespaces.
class Callbacks final : public PrintingCallbacks {
public:
bool isScopeVisible(const DeclContext *DC) const override {
return DC->getDeclKind() == Decl::Namespace;
}
} CB;

// Pick a good insertion location.
SourceLocation InsertionLoc;
const CXXMethodDecl *InsertAfterMethod = nullptr;
std::optional<NestedNameSpecifier> NamePrefix = std::nullopt;
if (DefinedOutOfLineMethods.empty()) {
PP.Callbacks = &CB;
const RecordDecl *OutermostRecord = findOutermostRecord(Class);
InsertionLoc = SM.getExpansionRange(OutermostRecord->getEndLoc()).getEnd();
if (SM.getFileID(InsertionLoc) == File) {
// We can insert right after the class. Compute the appropriate
// qualification.
NamePrefix = NestedNameSpecifier::getRequiredQualification(
Context, OutermostRecord->getLexicalDeclContext(),
Class->getLexicalDeclContext());
// We can insert right after the class.
} else {
// We can't insert after the end of the class, since the indexer told us
// that some file should have the implementation of it, even when there
// are no methods here. We should try to insert at the end of the file.
InsertionLoc = SM.getLocForEndOfFile(File);
NamePrefix = NestedNameSpecifier::getRequiredQualification(
Context, Context.getTranslationUnitDecl(),
Class->getLexicalDeclContext());
llvm::SmallVector<const NamespaceDecl *, 4> Namespaces;
std::optional<NestedNameSpecifier> Qualifier = NamePrefix;
while (Qualifier) {
auto [ND, Prefix] = Qualifier->getAsNamespaceAndPrefix();
if (ND)
Namespaces.push_back(ND->getNamespace());
Qualifier = Prefix;
}

for (const DeclContext *DC = OutermostRecord->getLexicalDeclContext(); DC;
DC = DC->getLookupParent())
if (auto *ND = dyn_cast<NamespaceDecl>(DC))
Namespaces.push_back(ND);

// When the class is in a namespace, add a 'using' declaration if it's
// needed and adjust the out-of-line qualification.
if (!Namespaces.empty()) {
Expand All @@ -305,9 +312,6 @@ ImplementDeclaredCXXMethodsOperation::runInImplementationAST(
}
OS << "\nusing namespace " << NamespaceOS.str() << ";";
}
// Re-compute the name qualifier without the namespace.
NamePrefix = NestedNameSpecifier::getRequiredQualification(
Context, InnermostNamespace, Class->getLexicalDeclContext());
}
}
} else {
Expand All @@ -324,12 +328,6 @@ ImplementDeclaredCXXMethodsOperation::runInImplementationAST(
InsertionLoc = getLastLineLocationUnlessItHasOtherTokens(
InsertionLoc, SM, Context.getLangOpts());

PrintingPolicy PP = Context.getPrintingPolicy();
PP.PolishForDeclaration = true;
PP.SupressStorageClassSpecifiers = true;
PP.SuppressStrongLifetime = true;
PP.SuppressLifetimeQualifiers = true;
PP.SuppressUnwrittenScope = true;
OS << "\n";
for (const auto &I : SelectedMethods) {
const CXXMethodDecl *MD = I.Decl;
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Tooling/Refactor/SymbolOccurrenceFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,9 @@ class SymbolOccurrenceFinderASTVisitor
checkDecl(TND, TTL.getNameLoc());
return true;
}
TypeSpecTypeLoc TSTL = Loc.getAs<TypeSpecTypeLoc>();
if (TSTL) {
checkDecl(Loc.getType()->getAsTagDecl(), TSTL.getNameLoc());
TagTypeLoc TagTL = Loc.getAs<TagTypeLoc>();
if (TagTL) {
checkDecl(Loc.getType()->getAsTagDecl(), TagTL.getNameLoc());
}
if (const auto *TemplateTypeParm =
dyn_cast<TemplateTypeParmType>(Loc.getType())) {
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Tooling/Refactor/USRFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,9 @@ class NamedDeclFindingASTVisitor
}
return checkOccurrence(TND, TTL.getNameLoc());
}
TypeSpecTypeLoc TSTL = Loc.getAs<TypeSpecTypeLoc>();
if (TSTL) {
return checkOccurrence(Loc.getType()->getAsTagDecl(), TSTL.getNameLoc());
TagTypeLoc TagTL = Loc.getAs<TagTypeLoc>();
if (TagTL) {
return checkOccurrence(Loc.getType()->getAsTagDecl(), TagTL.getNameLoc());
}
return true;
}
Expand Down