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 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 * resolveClassMutable (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 Definition * 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.
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 MemberDef * getTypedef () 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< Private > p

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 1698 of file symbolresolver.cpp.

1699 : p(std::make_unique<Private>(fileScope))
1700{
1701}
std::unique_ptr< Private > p

References p.

◆ ~SymbolResolver()

SymbolResolver::~SymbolResolver ( )

Definition at line 1703 of file symbolresolver.cpp.

1704{
1705}

Member Function Documentation

◆ clearTypeLookupCache()

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

Definition at line 1825 of file symbolresolver.cpp.

1826{
1827 auto &cache = getTypeLookupCache();
1828 switch (scope)
1829 {
1830 case ClearScope::All:
1831 cache.clear();
1832 break;
1834 {
1835 StringVector elementsToRemove;
1836 for (const auto &ci : cache)
1837 {
1838 const LookupInfo &li = ci.second;
1839 if (li.definition==nullptr && li.typeDef==nullptr)
1840 {
1841 elementsToRemove.push_back(ci.first);
1842 }
1843 }
1844 for (const auto &k : elementsToRemove)
1845 {
1846 cache.remove(k);
1847 }
1848 }
1849 break;
1851 {
1852 StringVector elementsToRemove;
1853 for (const auto &ci : cache)
1854 {
1855 const LookupInfo &li = ci.second;
1856 if (li.definition)
1857 {
1858 elementsToRemove.push_back(ci.first);
1859 }
1860 }
1861 for (const auto &k : elementsToRemove)
1862 {
1863 cache.remove(k);
1864 }
1865 }
1866 break;
1867 }
1868}
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 1820 of file symbolresolver.cpp.

1821{
1822 return p->resolvedType;
1823}

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 1815 of file symbolresolver.cpp.

1816{
1817 return p->templateSpec;
1818}

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 1810 of file symbolresolver.cpp.

1811{
1812 return p->typeDef;
1813}

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 1779 of file symbolresolver.cpp.

1780{
1781 AUTO_TRACE("scope={} item={}",
1782 scope?scope->name():DString(), item?item->name():DString());
1783 p->reset();
1784 VisitedKeys visitedKeys;
1785 AccessStack accessStack;
1786 int result = p->isAccessibleFrom(visitedKeys,accessStack,scope,item);
1787 AUTO_TRACE_EXIT("result={}",result);
1788 return result;
1789}
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 1791 of file symbolresolver.cpp.

1793{
1794 AUTO_TRACE("scope={} item={} explicitScopePart={}",
1795 scope?scope->name():DString(), item?item->name():DString(), explicitScopePart);
1796 p->reset();
1797 VisitedKeys visitedKeys;
1798 VisitedNamespaces visitedNamespaces;
1799 AccessStack accessStack;
1800 int result = p->isAccessibleFromWithExpScope(visitedKeys,visitedNamespaces,accessStack,scope,item,explicitScopePart);
1801 AUTO_TRACE_EXIT("result={}",result);
1802 return result;
1803}
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 1708 of file symbolresolver.cpp.

1712{
1713 AUTO_TRACE("scope={} name={} mayBeUnlinkable={} mayBeHidden={}",
1714 scope?scope->name():DString(), name, mayBeUnlinkable, mayBeHidden);
1715 p->reset();
1716
1717 auto lang = scope ? scope->getLanguage() :
1718 p->fileScope() ? p->fileScope()->getLanguage() :
1719 SrcLangExt::Cpp; // fallback to C++
1720
1721 if (scope==nullptr ||
1724 ) ||
1725 (name.stripWhiteSpace().startsWith("::")) ||
1726 ((lang==SrcLangExt::Java || lang==SrcLangExt::CSharp) && DString(name).find("::")!=DString::npos)
1727 )
1728 {
1730 }
1731 const ClassDef *result=nullptr;
1732 if (Config_getBool(OPTIMIZE_OUTPUT_VHDL))
1733 {
1734 result = getClass(name);
1735 }
1736 else
1737 {
1738 VisitedKeys visitedKeys;
1739 DString lookupName = lang==SrcLangExt::CSharp ? mangleCSharpGenericName(name) : name;
1740 AUTO_TRACE_ADD("lookup={}",lookupName);
1741 result = p->getResolvedTypeRec(getTypeLookupCache(),visitedKeys,scope,lookupName,&p->typeDef,&p->templateSpec,&p->resolvedType);
1742 if (result==nullptr) // for nested classes imported via tag files, the scope may not
1743 // present, so we check the class name directly as well.
1744 // See also bug701314
1745 {
1746 result = getClass(lookupName);
1747 }
1748 }
1749 if (!mayBeUnlinkable && result && !result->isLinkable())
1750 {
1751 if (!mayBeHidden || !result->isHidden())
1752 {
1753 AUTO_TRACE_ADD("hiding symbol {}",result->name());
1754 result=nullptr; // don't link to artificial/hidden classes unless explicitly allowed
1755 }
1756 }
1757 AUTO_TRACE_EXIT("result={}",result?result->name():DString());
1758 return result;
1759}
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:5335

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 1761 of file symbolresolver.cpp.

1767{
1768 AUTO_TRACE("scope={} name={} args={} checkCV={} insideCode={}",
1769 scope?scope->name():DString(), name, args, checkCV, insideCode);
1770 p->reset();
1771 if (scope==nullptr) scope=Doxygen::globalScope;
1772 VisitedKeys visitedKeys;
1773 const Definition *result = p->getResolvedSymbolRec(getSymbolLookupCache(),visitedKeys,scope,name,args,checkCV,insideCode,onlyLinkable,&p->typeDef,&p->templateSpec,&p->resolvedType);
1774 AUTO_TRACE_EXIT("result={}{}", qPrint(result?result->qualifiedName():DString()),
1775 qPrint(result && result->definitionType()==Definition::TypeMember ? toMemberDef(result)->argsString() : DString()));
1776 return result;
1777}
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 1805 of file symbolresolver.cpp.

1806{
1807 p->setFileScope(fileScope);
1808}

References p.

Referenced by getDefs().

◆ showCacheUsage()

void SymbolResolver::showCacheUsage ( )
static

Show usage of the type lookup cache.

Definition at line 1886 of file symbolresolver.cpp.

1887{
1888 // merge the stats of the main thread
1891
1892 std::lock_guard lock1(g_typeCacheStatistics.mutex);
1893 std::lock_guard lock2(g_symbolCacheStatistics.mutex);
1894
1895 msg("type lookup cache used {}/{} hits={} misses={}\n",
1900 msg("symbol lookup cache used {}/{} hits={} misses={}\n",
1905 int typeCacheParam = computeIdealCacheParam(static_cast<size_t>(g_typeCacheStatistics.misses*2/3)); // part of the cache is flushed, hence the 2/3 correction factor
1906 int symbolCacheParam = computeIdealCacheParam(static_cast<size_t>(g_symbolCacheStatistics.misses*2/3)); // part of the cache is flushed, hence the 2/3 correction factor
1907 int cacheParam = std::max(typeCacheParam,symbolCacheParam);
1908 if (cacheParam>Config_getInt(LOOKUP_CACHE_SIZE))
1909 {
1910 msg("Note: based on cache misses the ideal setting for LOOKUP_CACHE_SIZE is {} at the cost of higher memory usage.\n",cacheParam);
1911 }
1912}
#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: