cmTarget: Make GetLink*Libraries methods safer to use

Split the library lists out of LinkImplementation and LinkInterface into
LinkImplementationLibraries and LinkInterfaceLibraries parent classes,
respectively.  Return these from GetLinkImplementationLibraries and
GetLinkInterfaceLibraries, respectively, so that callers cannot access
parts of the structures that have not been populated.
This commit is contained in:
Brad King 2014-07-15 16:12:07 -04:00
parent 6e7e881c57
commit 535fd6ce6d
3 changed files with 39 additions and 32 deletions

View File

@ -1114,7 +1114,7 @@ static const struct TargetPropertyNode : public cmGeneratorExpressionNode
else if (std::find_if(transBegin, transEnd, else if (std::find_if(transBegin, transEnd,
cmStrCmp(interfacePropertyName)) != transEnd) cmStrCmp(interfacePropertyName)) != transEnd)
{ {
const cmTarget::LinkImplementation *impl const cmTarget::LinkImplementationLibraries *impl
= target->GetLinkImplementationLibraries(context->Config); = target->GetLinkImplementationLibraries(context->Config);
if(impl) if(impl)
{ {

View File

@ -4379,7 +4379,7 @@ bool cmTarget::HaveBuildTreeRPATH(const std::string& config) const
{ {
return false; return false;
} }
if(LinkImplementation const* impl = if(LinkImplementationLibraries const* impl =
this->GetLinkImplementationLibraries(config)) this->GetLinkImplementationLibraries(config))
{ {
return !impl->Libraries.empty(); return !impl->Libraries.empty();
@ -5935,7 +5935,7 @@ cmTarget::LinkInterface const* cmTarget::GetLinkInterface(
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
cmTarget::LinkInterface const* cmTarget::LinkInterfaceLibraries const*
cmTarget::GetLinkInterfaceLibraries(const std::string& config, cmTarget::GetLinkInterfaceLibraries(const std::string& config,
cmTarget const* head, cmTarget const* head,
bool usage_requirements_only) const bool usage_requirements_only) const
@ -6017,7 +6017,7 @@ void processILibs(const std::string& config,
if (item.Target && emitted.insert(item.Target).second) if (item.Target && emitted.insert(item.Target).second)
{ {
tgts.push_back(item.Target); tgts.push_back(item.Target);
if(cmTarget::LinkInterface const* iface = if(cmTarget::LinkInterfaceLibraries const* iface =
item.Target->GetLinkInterfaceLibraries(config, headTarget, true)) item.Target->GetLinkInterfaceLibraries(config, headTarget, true))
{ {
for(std::vector<cmLinkItem>::const_iterator for(std::vector<cmLinkItem>::const_iterator
@ -6041,7 +6041,7 @@ cmTarget::GetLinkImplementationClosure(const std::string& config) const
this->Internal->CacheLinkImplementationClosureDone[config] = true; this->Internal->CacheLinkImplementationClosureDone[config] = true;
std::set<cmTarget const*> emitted; std::set<cmTarget const*> emitted;
cmTarget::LinkImplementation const* impl cmTarget::LinkImplementationLibraries const* impl
= this->GetLinkImplementationLibraries(config); = this->GetLinkImplementationLibraries(config);
for(std::vector<cmLinkImplItem>::const_iterator for(std::vector<cmLinkImplItem>::const_iterator
@ -6059,7 +6059,7 @@ void cmTarget::GetTransitivePropertyTargets(const std::string& config,
cmTarget const* headTarget, cmTarget const* headTarget,
std::vector<cmTarget const*> &tgts) const std::vector<cmTarget const*> &tgts) const
{ {
if(cmTarget::LinkInterface const* iface = if(cmTarget::LinkInterfaceLibraries const* iface =
this->GetLinkInterfaceLibraries(config, headTarget, true)) this->GetLinkInterfaceLibraries(config, headTarget, true))
{ {
for(std::vector<cmLinkItem>::const_iterator it = iface->Libraries.begin(); for(std::vector<cmLinkItem>::const_iterator it = iface->Libraries.begin();
@ -6177,7 +6177,7 @@ cmTargetInternals::ComputeLinkInterfaceLibraries(
// to the link implementation. // to the link implementation.
{ {
// The link implementation is the default link interface. // The link implementation is the default link interface.
cmTarget::LinkImplementation const* impl = cmTarget::LinkImplementationLibraries const* impl =
thisTarget->GetLinkImplementationLibrariesInternal(config, headTarget); thisTarget->GetLinkImplementationLibrariesInternal(config, headTarget);
std::copy(impl->Libraries.begin(), impl->Libraries.end(), std::copy(impl->Libraries.begin(), impl->Libraries.end(),
std::back_inserter(iface.Libraries)); std::back_inserter(iface.Libraries));
@ -6294,7 +6294,7 @@ void cmTargetInternals::ComputeLinkInterface(cmTarget const* thisTarget,
|| thisTarget->PolicyStatusCMP0022 == cmPolicies::OLD) || thisTarget->PolicyStatusCMP0022 == cmPolicies::OLD)
{ {
// The link implementation is the default link interface. // The link implementation is the default link interface.
cmTarget::LinkImplementation const* cmTarget::LinkImplementationLibraries const*
impl = thisTarget->GetLinkImplementationLibrariesInternal(config, impl = thisTarget->GetLinkImplementationLibrariesInternal(config,
headTarget); headTarget);
iface.ImplementationIsInterface = true; iface.ImplementationIsInterface = true;
@ -6345,7 +6345,7 @@ void cmTargetInternals::AddInterfaceEntries(
cmTarget const* thisTarget, std::string const& config, cmTarget const* thisTarget, std::string const& config,
std::string const& prop, std::vector<TargetPropertyEntry*>& entries) std::string const& prop, std::vector<TargetPropertyEntry*>& entries)
{ {
if(cmTarget::LinkImplementation const* impl = if(cmTarget::LinkImplementationLibraries const* impl =
thisTarget->GetLinkImplementationLibraries(config)) thisTarget->GetLinkImplementationLibraries(config))
{ {
for (std::vector<cmLinkImplItem>::const_iterator for (std::vector<cmLinkImplItem>::const_iterator
@ -6383,7 +6383,7 @@ cmTarget::GetLinkImplementation(const std::string& config) const
if(!impl.LibrariesDone) if(!impl.LibrariesDone)
{ {
impl.LibrariesDone = true; impl.LibrariesDone = true;
this->ComputeLinkImplementation(config, impl, this); this->ComputeLinkImplementationLibraries(config, impl, this);
} }
if(!impl.LanguagesDone) if(!impl.LanguagesDone)
{ {
@ -6394,14 +6394,14 @@ cmTarget::GetLinkImplementation(const std::string& config) const
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
cmTarget::LinkImplementation const* cmTarget::LinkImplementationLibraries const*
cmTarget::GetLinkImplementationLibraries(const std::string& config) const cmTarget::GetLinkImplementationLibraries(const std::string& config) const
{ {
return this->GetLinkImplementationLibrariesInternal(config, this); return this->GetLinkImplementationLibrariesInternal(config, this);
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
cmTarget::LinkImplementation const* cmTarget::LinkImplementationLibraries const*
cmTarget::GetLinkImplementationLibrariesInternal(const std::string& config, cmTarget::GetLinkImplementationLibrariesInternal(const std::string& config,
cmTarget const* head) const cmTarget const* head) const
{ {
@ -6418,15 +6418,16 @@ cmTarget::GetLinkImplementationLibrariesInternal(const std::string& config,
if(!impl.LibrariesDone) if(!impl.LibrariesDone)
{ {
impl.LibrariesDone = true; impl.LibrariesDone = true;
this->ComputeLinkImplementation(config, impl, head); this->ComputeLinkImplementationLibraries(config, impl, head);
} }
return &impl; return &impl;
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
void cmTarget::ComputeLinkImplementation(const std::string& config, void
LinkImplementation& impl, cmTarget::ComputeLinkImplementationLibraries(const std::string& config,
cmTarget const* head) const LinkImplementation& impl,
cmTarget const* head) const
{ {
// Collect libraries directly linked in this configuration. // Collect libraries directly linked in this configuration.
for (std::vector<cmValueWithOrigin>::const_iterator for (std::vector<cmValueWithOrigin>::const_iterator

View File

@ -262,14 +262,16 @@ public:
/** The link interface specifies transitive library dependencies and /** The link interface specifies transitive library dependencies and
other information needed by targets that link to this target. */ other information needed by targets that link to this target. */
struct LinkInterface struct LinkInterfaceLibraries
{
// Libraries listed in the interface.
std::vector<cmLinkItem> Libraries;
};
struct LinkInterface: public LinkInterfaceLibraries
{ {
// Languages whose runtime libraries must be linked. // Languages whose runtime libraries must be linked.
std::vector<std::string> Languages; std::vector<std::string> Languages;
// Libraries listed in the interface.
std::vector<cmLinkItem> Libraries;
// Shared library dependencies needed for linking on some platforms. // Shared library dependencies needed for linking on some platforms.
std::vector<cmLinkItem> SharedDeps; std::vector<cmLinkItem> SharedDeps;
@ -290,22 +292,21 @@ public:
if the target cannot be linked. */ if the target cannot be linked. */
LinkInterface const* GetLinkInterface(const std::string& config, LinkInterface const* GetLinkInterface(const std::string& config,
cmTarget const* headTarget) const; cmTarget const* headTarget) const;
LinkInterface const* GetLinkInterfaceLibraries(const std::string& config, LinkInterfaceLibraries const*
cmTarget const* headTarget, GetLinkInterfaceLibraries(const std::string& config,
bool usage_requirements_only) const; cmTarget const* headTarget,
bool usage_requirements_only) const;
void GetTransitivePropertyTargets(const std::string& config, void GetTransitivePropertyTargets(const std::string& config,
cmTarget const* headTarget, cmTarget const* headTarget,
std::vector<cmTarget const*> &libs) const; std::vector<cmTarget const*> &libs) const;
std::vector<cmTarget const*> const& std::vector<cmTarget const*> const&
GetLinkImplementationClosure(const std::string& config) const; GetLinkImplementationClosure(const std::string& config) const;
/** The link implementation specifies the direct library /** The link implementation specifies the direct library
dependencies needed by the object files of the target. */ dependencies needed by the object files of the target. */
struct LinkImplementation struct LinkImplementationLibraries
{ {
// Languages whose runtime libraries must be linked.
std::vector<std::string> Languages;
// Libraries linked directly in this configuration. // Libraries linked directly in this configuration.
std::vector<cmLinkImplItem> Libraries; std::vector<cmLinkImplItem> Libraries;
@ -313,10 +314,15 @@ public:
// Needed only for OLD behavior of CMP0003. // Needed only for OLD behavior of CMP0003.
std::vector<cmLinkItem> WrongConfigLibraries; std::vector<cmLinkItem> WrongConfigLibraries;
}; };
struct LinkImplementation: public LinkImplementationLibraries
{
// Languages whose runtime libraries must be linked.
std::vector<std::string> Languages;
};
LinkImplementation const* LinkImplementation const*
GetLinkImplementation(const std::string& config) const; GetLinkImplementation(const std::string& config) const;
LinkImplementation const* LinkImplementationLibraries const*
GetLinkImplementationLibraries(const std::string& config) const; GetLinkImplementationLibraries(const std::string& config) const;
/** Link information from the transitive closure of the link /** Link information from the transitive closure of the link
@ -778,12 +784,12 @@ private:
GetImportLinkInterface(const std::string& config, cmTarget const* head, GetImportLinkInterface(const std::string& config, cmTarget const* head,
bool usage_requirements_only) const; bool usage_requirements_only) const;
LinkImplementation const* LinkImplementationLibraries const*
GetLinkImplementationLibrariesInternal(const std::string& config, GetLinkImplementationLibrariesInternal(const std::string& config,
cmTarget const* head) const; cmTarget const* head) const;
void ComputeLinkImplementation(const std::string& config, void ComputeLinkImplementationLibraries(const std::string& config,
LinkImplementation& impl, LinkImplementation& impl,
cmTarget const* head) const; cmTarget const* head) const;
void ComputeLinkImplementationLanguages(const std::string& config, void ComputeLinkImplementationLanguages(const std::string& config,
LinkImplementation& impl) const; LinkImplementation& impl) const;
void ComputeLinkClosure(const std::string& config, LinkClosure& lc) const; void ComputeLinkClosure(const std::string& config, LinkClosure& lc) const;