Doxygen
Loading...
Searching...
No Matches
SymbolResolver Class Reference

#include <src/symbolresolver.h>

Classes

struct  Private

Public Types

enum class  ClearScope { All , Classes , Unresolved }
 Clear the type lookup cache for the current thread. More...

Public Member Functions

 SymbolResolver (const FileDef *fileScope=nullptr)
 ~SymbolResolver ()
const ClassDefresolveClass (const Definition *scope, const DString &name, bool maybeUnlinkable=false, bool mayBeHidden=false)
 Find the class definition matching name within the scope set.
ClassDefMutableresolveClassMutable (const Definition *scope, const DString &name, bool mayBeUnlinkable=false, bool mayBeHidden=false)
 Wrapper around resolveClass that returns a mutable interface to the class object or a nullptr if the symbol is immutable.
const DefinitionresolveSymbol (const Definition *scope, const DString &name, const DString &args=DString(), bool checkCV=false, bool insideCode=false, bool onlyLinkable=false)
 Find the symbool definition matching name within the scope set.
int isAccessibleFrom (const Definition *scope, const Definition *item)
 Checks if symbol item is accessible from within scope.
int isAccessibleFromWithExpScope (const Definition *scope, const Definition *item, const DString &explicitScopePart)
 Check if symbol item is accessible from within scope, where it has to match the explicitScopePart.
void setFileScope (const FileDef *fd)
 Sets or updates the file scope using when resolving symbols.
const MemberDefgetTypedef () const
 In case a call to resolveClass() resolves to a type member (e.g. an enum) this method will return it.
DString getTemplateSpec () const
 In case a call to resolveClass() points to a template specialization, the template part is return via this method.
DString getResolvedType () const
 In case a call to resolveClass() points to a typedef or using declaration.

Static Public Member Functions

static void clearTypeLookupCache (ClearScope scope)
static void showCacheUsage ()
 Show usage of the type lookup cache.

Private Attributes

std::unique_ptr< Privatep

Detailed Description

Helper class to find a class definition or check if A symbol is accessible in a given scope.

Definition at line 31 of file symbolresolver.h.

Member Enumeration Documentation

◆ ClearScope

enum class SymbolResolver::ClearScope
strong

Clear the type lookup cache for the current thread.

Enumerator
All 
Classes 
Unresolved 

Definition at line 102 of file symbolresolver.h.

102{ All, Classes, Unresolved };

Constructor & Destructor Documentation

◆ SymbolResolver()

SymbolResolver::SymbolResolver ( const FileDef * fileScope = nullptr)
explicit

Definition at line 1687 of file symbolresolver.cpp.

1688 : p(std::make_unique<Private>(fileScope))
1689{
1690}
std::unique_ptr< Private > p

References p.

◆ ~SymbolResolver()

SymbolResolver::~SymbolResolver ( )

Definition at line 1692 of file symbolresolver.cpp.

1693{
1694}

Member Function Documentation

◆ clearTypeLookupCache()

void SymbolResolver::clearTypeLookupCache ( SymbolResolver::ClearScope scope)
static

Definition at line 1814 of file symbolresolver.cpp.

1815{
1816 auto &cache = getTypeLookupCache();
1817 switch (scope)
1818 {
1819 case ClearScope::All:
1820 cache.clear();
1821 break;
1823 {
1824 StringVector elementsToRemove;
1825 for (const auto &ci : cache)
1826 {
1827 const LookupInfo &li = ci.second;
1828 if (li.definition==nullptr && li.typeDef==nullptr)
1829 {
1830 elementsToRemove.push_back(ci.first);
1831 }
1832 }
1833 for (const auto &k : elementsToRemove)
1834 {
1835 cache.remove(k);
1836 }
1837 }
1838 break;
1840 {
1841 StringVector elementsToRemove;
1842 for (const auto &ci : cache)
1843 {
1844 const LookupInfo &li = ci.second;
1845 if (li.definition)
1846 {
1847 elementsToRemove.push_back(ci.first);
1848 }
1849 }
1850 for (const auto &k : elementsToRemove)
1851 {
1852 cache.remove(k);
1853 }
1854 }
1855 break;
1856 }
1857}
std::vector< std::string > StringVector
Definition containers.h:33
const Definition * definition
Definition doxygen.h:52
const MemberDef * typeDef
Definition doxygen.h:53
static LookupCache & getTypeLookupCache()

References All, Classes, LookupInfo::definition, getTypeLookupCache(), LookupInfo::typeDef, and Unresolved.

Referenced by flushCachedTemplateRelations(), flushUnresolvedRelations(), and parseInput().

◆ getResolvedType()

DString SymbolResolver::getResolvedType ( ) const

In case a call to resolveClass() points to a typedef or using declaration.

The type name it resolved to is returned via this method.

Definition at line 1809 of file symbolresolver.cpp.

1810{
1811 return p->resolvedType;
1812}

References p.

Referenced by getCanonicalTypeForIdentifier(), and SymbolResolver::Private::getResolvedTypeRec().

◆ getTemplateSpec()

DString SymbolResolver::getTemplateSpec ( ) const

In case a call to resolveClass() points to a template specialization, the template part is return via this method.

Definition at line 1804 of file symbolresolver.cpp.

1805{
1806 return p->templateSpec;
1807}

References p.

Referenced by findClassRelation(), and getCanonicalTypeForIdentifier().

◆ getTypedef()

const MemberDef * SymbolResolver::getTypedef ( ) const

In case a call to resolveClass() resolves to a type member (e.g. an enum) this method will return it.

Definition at line 1799 of file symbolresolver.cpp.

1800{
1801 return p->typeDef;
1802}

References p.

Referenced by findClassRelation(), getCanonicalTypeForIdentifier(), and linkifyText().

◆ isAccessibleFrom()

int SymbolResolver::isAccessibleFrom ( const Definition * scope,
const Definition * item )

Checks if symbol item is accessible from within scope.

Returns
-1 if item is not accessible or a number indicating how many scope levels up the nearest match was found.

Definition at line 1768 of file symbolresolver.cpp.

1769{
1770 AUTO_TRACE("scope={} item={}",
1771 scope?scope->name():DString(), item?item->name():DString());
1772 p->reset();
1773 VisitedKeys visitedKeys;
1774 AccessStack accessStack;
1775 int result = p->isAccessibleFrom(visitedKeys,accessStack,scope,item);
1776 AUTO_TRACE_EXIT("result={}",result);
1777 return result;
1778}
virtual const DString & name() const =0
#define AUTO_TRACE(...)
Definition docnode.cpp:53
#define AUTO_TRACE_EXIT(...)
Definition docnode.cpp:55
StringVector VisitedKeys

References AUTO_TRACE, AUTO_TRACE_EXIT, Definition::name(), and p.

Referenced by SymbolResolver::Private::isAccessibleFrom(), SymbolResolver::Private::isAccessibleFromWithExpScope(), and resolveTypeDef().

◆ isAccessibleFromWithExpScope()

int SymbolResolver::isAccessibleFromWithExpScope ( const Definition * scope,
const Definition * item,
const DString & explicitScopePart )

Check if symbol item is accessible from within scope, where it has to match the explicitScopePart.

Returns
-1 if item is not accessible or a number indicating how many scope levels up the nearest match was found.

Definition at line 1780 of file symbolresolver.cpp.

1782{
1783 AUTO_TRACE("scope={} item={} explicitScopePart={}",
1784 scope?scope->name():DString(), item?item->name():DString(), explicitScopePart);
1785 p->reset();
1786 VisitedKeys visitedKeys;
1787 VisitedNamespaces visitedNamespaces;
1788 AccessStack accessStack;
1789 int result = p->isAccessibleFromWithExpScope(visitedKeys,visitedNamespaces,accessStack,scope,item,explicitScopePart);
1790 AUTO_TRACE_EXIT("result={}",result);
1791 return result;
1792}
std::unordered_map< std::string, const Definition * > VisitedNamespaces

References AUTO_TRACE, AUTO_TRACE_EXIT, Definition::name(), and p.

Referenced by getMemberFromSymbol(), SymbolResolver::Private::getResolvedSymbol(), SymbolResolver::Private::getResolvedType(), SymbolResolver::Private::isAccessibleFromWithExpScope(), and SymbolResolver::Private::substTypedef().

◆ resolveClass()

const ClassDef * SymbolResolver::resolveClass ( const Definition * scope,
const DString & name,
bool maybeUnlinkable = false,
bool mayBeHidden = false )

Find the class definition matching name within the scope set.

Parameters
scopeThe scope to search from.
nameThe name of the symbol.
maybeUnlinkableinclude unlinkable symbols in the search.
mayBeHiddeninclude hidden symbols in the search.
Note
As a result of this call the getters getTypedef(), getTemplateSpec(), and getResolvedType() are set as well.

Definition at line 1697 of file symbolresolver.cpp.

1701{
1702 AUTO_TRACE("scope={} name={} mayBeUnlinkable={} mayBeHidden={}",
1703 scope?scope->name():DString(), name, mayBeUnlinkable, mayBeHidden);
1704 p->reset();
1705
1706 auto lang = scope ? scope->getLanguage() :
1707 p->fileScope() ? p->fileScope()->getLanguage() :
1708 SrcLangExt::Cpp; // fallback to C++
1709
1710 if (scope==nullptr ||
1713 ) ||
1714 (name.stripWhiteSpace().startsWith("::")) ||
1715 ((lang==SrcLangExt::Java || lang==SrcLangExt::CSharp) && DString(name).find("::")!=DString::npos)
1716 )
1717 {
1719 }
1720 const ClassDef *result=nullptr;
1721 if (Config_getBool(OPTIMIZE_OUTPUT_VHDL))
1722 {
1723 result = getClass(name);
1724 }
1725 else
1726 {
1727 VisitedKeys visitedKeys;
1728 DString lookupName = lang==SrcLangExt::CSharp ? mangleCSharpGenericName(name) : name;
1729 AUTO_TRACE_ADD("lookup={}",lookupName);
1730 result = p->getResolvedTypeRec(getTypeLookupCache(),visitedKeys,scope,lookupName,&p->typeDef,&p->templateSpec,&p->resolvedType);
1731 if (result==nullptr) // for nested classes imported via tag files, the scope may not
1732 // present, so we check the class name directly as well.
1733 // See also bug701314
1734 {
1735 result = getClass(lookupName);
1736 }
1737 }
1738 if (!mayBeUnlinkable && result && !result->isLinkable())
1739 {
1740 if (!mayBeHidden || !result->isHidden())
1741 {
1742 AUTO_TRACE_ADD("hiding symbol {}",result->name());
1743 result=nullptr; // don't link to artificial/hidden classes unless explicitly allowed
1744 }
1745 }
1746 AUTO_TRACE_EXIT("result={}",result?result->name():DString());
1747 return result;
1748}
static constexpr size_t npos
value used to indicate 'not found' or 'to the end of the string', matching std::string::npos
Definition dstring.h:178
DString stripWhiteSpace() const
returns a copy of this string with leading and trailing whitespace removed
Definition dstring.h:337
bool startsWith(const char *s) const
Definition dstring.h:600
virtual SrcLangExt getLanguage() const =0
Returns the programming language this definition was written in.
virtual bool isLinkable() const =0
virtual DefType definitionType() const =0
virtual bool isHidden() const =0
static NamespaceDefMutable * globalScope
Definition doxygen.h:114
ClassDef * getClass(const DString &n)
#define Config_getBool(name)
Definition config.h:33
#define AUTO_TRACE_ADD(...)
Definition docnode.cpp:54
DString mangleCSharpGenericName(const DString &name)
Definition util.cpp:5319

References AUTO_TRACE, AUTO_TRACE_ADD, AUTO_TRACE_EXIT, Config_getBool, Definition::definitionType(), getClass(), Definition::getLanguage(), getTypeLookupCache(), Doxygen::globalScope, Definition::isHidden(), Definition::isLinkable(), mangleCSharpGenericName(), Definition::name(), DString::npos, p, DString::startsWith(), DString::stripWhiteSpace(), Definition::TypeClass, and Definition::TypeNamespace.

Referenced by findClassDefinition(), findClassWithinClassContext(), findUsingDeclImports(), getCanonicalTypeForIdentifier(), isVarWithConstructor(), linkifyText(), normalizeNonTemplateArgumentsInString(), and resolveClassMutable().

◆ resolveClassMutable()

ClassDefMutable * SymbolResolver::resolveClassMutable ( const Definition * scope,
const DString & name,
bool mayBeUnlinkable = false,
bool mayBeHidden = false )
inline

Wrapper around resolveClass that returns a mutable interface to the class object or a nullptr if the symbol is immutable.

Definition at line 57 of file symbolresolver.h.

61 {
62 return toClassDefMutable(const_cast<ClassDef*>(resolveClass(scope,name,mayBeUnlinkable,mayBeHidden)));
63 }
const ClassDef * resolveClass(const Definition *scope, const DString &name, bool maybeUnlinkable=false, bool mayBeHidden=false)
Find the class definition matching name within the scope set.
ClassDefMutable * toClassDefMutable(Definition *d)

References resolveClass(), and toClassDefMutable().

Referenced by ClassDefImpl::addTypeConstraint(), findClassRelation(), findUsedClassesForClass(), and resolveTemplateInstanceInType().

◆ resolveSymbol()

const Definition * SymbolResolver::resolveSymbol ( const Definition * scope,
const DString & name,
const DString & args = DString(),
bool checkCV = false,
bool insideCode = false,
bool onlyLinkable = false )

Find the symbool definition matching name within the scope set.

Parameters
scopeThe scope to search from.
nameThe name of the symbol.
argsArgument list associated with the symbol (for functions)
checkCVCheck const/volatile qualifiers (for methods)
insideCodeIs the symbol found in a code fragment
onlyLinkableOnly search linkable definitions

Definition at line 1750 of file symbolresolver.cpp.

1756{
1757 AUTO_TRACE("scope={} name={} args={} checkCV={} insideCode={}",
1758 scope?scope->name():DString(), name, args, checkCV, insideCode);
1759 p->reset();
1760 if (scope==nullptr) scope=Doxygen::globalScope;
1761 VisitedKeys visitedKeys;
1762 const Definition *result = p->getResolvedSymbolRec(getSymbolLookupCache(),visitedKeys,scope,name,args,checkCV,insideCode,onlyLinkable,&p->typeDef,&p->templateSpec,&p->resolvedType);
1763 AUTO_TRACE_EXIT("result={}{}", qPrint(result?result->qualifiedName():DString()),
1764 qPrint(result && result->definitionType()==Definition::TypeMember ? toMemberDef(result)->argsString() : DString()));
1765 return result;
1766}
virtual DString qualifiedName() const =0
const char * qPrint(const char *s)
Definition dstring.h:783
MemberDef * toMemberDef(Definition *d)
static LookupCache & getSymbolLookupCache()

References AUTO_TRACE, AUTO_TRACE_EXIT, Definition::definitionType(), getSymbolLookupCache(), Doxygen::globalScope, Definition::name(), p, qPrint(), Definition::qualifiedName(), toMemberDef(), and Definition::TypeMember.

Referenced by findUsingDeclarations(), findUsingDeclImports(), getDefs(), and linkifyText().

◆ setFileScope()

void SymbolResolver::setFileScope ( const FileDef * fd)

Sets or updates the file scope using when resolving symbols.

Definition at line 1794 of file symbolresolver.cpp.

1795{
1796 p->setFileScope(fileScope);
1797}

References p.

Referenced by getDefs().

◆ showCacheUsage()

void SymbolResolver::showCacheUsage ( )
static

Show usage of the type lookup cache.

Definition at line 1875 of file symbolresolver.cpp.

1876{
1877 // merge the stats of the main thread
1880
1881 std::lock_guard lock1(g_typeCacheStatistics.mutex);
1882 std::lock_guard lock2(g_symbolCacheStatistics.mutex);
1883
1884 msg("type lookup cache used {}/{} hits={} misses={}\n",
1889 msg("symbol lookup cache used {}/{} hits={} misses={}\n",
1894 int typeCacheParam = computeIdealCacheParam(static_cast<size_t>(g_typeCacheStatistics.misses*2/3)); // part of the cache is flushed, hence the 2/3 correction factor
1895 int symbolCacheParam = computeIdealCacheParam(static_cast<size_t>(g_symbolCacheStatistics.misses*2/3)); // part of the cache is flushed, hence the 2/3 correction factor
1896 int cacheParam = std::max(typeCacheParam,symbolCacheParam);
1897 if (cacheParam>Config_getInt(LOOKUP_CACHE_SIZE))
1898 {
1899 msg("Note: based on cache misses the ideal setting for LOOKUP_CACHE_SIZE is {} at the cost of higher memory usage.\n",cacheParam);
1900 }
1901}
#define Config_getInt(name)
Definition config.h:34
#define msg(fmt,...)
Definition message.h:94
static void mergeStatistics(CacheStatistics &stats, LookupCache &cache)
static int computeIdealCacheParam(size_t v)
static CacheStatistics g_symbolCacheStatistics
static CacheStatistics g_typeCacheStatistics

References CacheStatistics::capacity, computeIdealCacheParam(), Config_getInt, g_symbolCacheStatistics, g_typeCacheStatistics, getSymbolLookupCache(), getTypeLookupCache(), CacheStatistics::hits, mergeStatistics(), CacheStatistics::misses, msg, CacheStatistics::mutex, and CacheStatistics::size.

Referenced by generateOutput().

Member Data Documentation

◆ p


The documentation for this class was generated from the following files: