Commit e3bafb16 authored by Christian Dietrich's avatar Christian Dietrich
Browse files

Ignore Scratch Space Locations

parent 76546497
......@@ -112,14 +112,15 @@ def nesting_analysis():
K('expansions/max children', max([m[1] for m in nests]))
K('expansions/max depth', max([m[2] for m in nests]))
for m in nests:
if m[1] == 1448:
if m[1] == 637:
print(m)
if m[2] == 185:
if m[2] == 15:
print(m)
nesting_analysis()
sys.exit()
def format_sig(name, sig):
sig = eval(sig)
......
......@@ -109,19 +109,17 @@ void MacroForest::MacroExpands(const Token &MacroNameTok,
Expansion->SpellingRange = SpellingRange;
Expansion->MI = MI;
// auto &SM = Context.getSourceManager();
auto &SM = Context.getSourceManager();
// llvm::errs() << "----- " << Expansion->Name << " "
// << inMacroArgExpansion.size() <<"("
// << (inMacroArgExpansion.size() ? inMacroArgExpansion.back() :-1)
// << ") ";
// SpellingRange.dump(SM);
// llvm::errs() << "RANGE ";
// Range.dump(SM);
/// for (const auto Node : InvocationStack) {
/// llvm::errs() << Node->Name << " ";
/// Node->SpellingRange.dump(SM);
/// }
// for (const auto Node : InvocationStack) {
// llvm::errs() << Node->Name << " ";
// Node->SpellingRange.dump(SM);
// }
// ATTENTION: If we are in a macro-argument expansion, we have to
......@@ -140,6 +138,12 @@ void MacroForest::MacroExpands(const Token &MacroNameTok,
unsigned protected_entries = (inMacroArgExpansion.size()
? inMacroArgExpansion.back() -1
: 0);
// If the current spelling range is not located in the file (e.g.,
// foo_##ARG##_bar), we do not pop anything
if (SM.isWrittenInScratchSpace(SpellingRange.getEnd())){
protected_entries = InvocationStack.size();
}
while (InvocationStack.size() > protected_entries) {
// ATTENTION!: We have to look only at the DefinitionRange of
// the parent node. If we would look at the SpellingRange
......@@ -149,7 +153,7 @@ void MacroForest::MacroExpands(const Token &MacroNameTok,
if (InvocationStack.back()->DefinitionRange.fullyContains(SpellingRange.getEnd()))
break;
// llvm::errs() << "pop\n";
//llvm::errs() << "pop\n";
InvocationStack.pop_back();
}
......
......@@ -121,10 +121,12 @@ template <>
inline SourceLocation getSpecificLocation<DynTypedNode>(const DynTypedNode &Node) {
if (const Stmt *S = Node.get<clang::Stmt>())
return getSpecificLocation(*S);
else if (const Decl *S = Node.get<clang::Decl>())
return getSpecificLocation(*S);
else if (const TypeLoc *S = Node.get<clang::TypeLoc>())
return getSpecificLocation(*S);
// Actually this is Incorrect. However, as long as our complete match chain does not
// support all this, this brings more problems than it solves. (Top-Level Defintions)
//else if (const Decl *S = Node.get<clang::Decl>())
// return getSpecificLocation(*S);
//else if (const TypeLoc *S = Node.get<clang::TypeLoc>())
// return getSpecificLocation(*S);
else
return SourceLocation();
}
......@@ -160,12 +162,18 @@ AST_POLYMORPHIC_MATCHER(isExpansionRoot,
for (const auto &Parent : Context.getParents(Node)) {
SourceLocation PLoc = getSpecificLocation(Parent);
SourceLocation PELoc = Context.getFullLoc(PLoc).getExpansionLoc();
// Parent comes from the same expansion as the child
// => Our node cannot be an expansion root
if (ELoc == PELoc) {
return false;
}
//PELoc.dump(SM);
//Parent.dump(llvm::errs(), Context);
}
//ELoc.dump(SM);
//Node.dump();
return true;
}
......
......@@ -221,16 +221,9 @@ public:
this->Context = &Context;
SourceManager &SM = Context.getSourceManager();
std::set<std::string> focus_macros;
// focus_macros.insert("raw_spin_lock_bh");
// Remove all Macro Roots that are not expanded in the main file
Roots.erase(
std::remove_if(Roots.begin(), Roots.end(), [&SM, &focus_macros](const MacroForest::Node *N) {
// If a Macro is in the Focus Group: Process it, even
// if it is used in a header file.
if (focus_macros.find(N->Name) != focus_macros.end())
return false;
std::remove_if(Roots.begin(), Roots.end(), [&SM](const MacroForest::Node *N) {
// Only look at source files
SourceLocation Loc = N->SpellingRange.getBegin();
return !SM.isInMainFile(Loc) || SM.isWrittenInScratchSpace(Loc);
......@@ -257,28 +250,34 @@ public:
SourceLocation ExpansionLoc = SM.getExpansionLoc(ST->getBeginLoc());
MacroForest::Node *ExpansionRoot = nullptr;
for (auto E : Roots) {
if (E->SpellingRange.fullyContains(ExpansionLoc)) {
// Check if the ExpansionRoot and the Node have the
// same Expansion Location. Previously, we checked if
// the ExpansionLoc was contained in the Spelling
// Range. However, this might even span files if macro
// name and argument list are composed in a macro.
SourceLocation NodeExpansionLoc =
SM.getExpansionLoc(E->SpellingRange.getBegin());
if (NodeExpansionLoc == ExpansionLoc) {
ExpansionRoot = E;
break;
}
}
if (ExpansionRoot == nullptr) {
StringRef Name = clang::Lexer::getImmediateMacroName(ST->getBeginLoc(), SM, m_lo);
bool focus = focus_macros.find(std::string(Name)) != focus_macros.end();
focus ? llvm::errs() : VS << " Skipped macro expansion "
<< Name << "\n";
VS << " Skipped macro expansion "
<< Name << "\n";
continue;
}
bool focus = focus_macros.find(ExpansionRoot->Name) != focus_macros.end();
(focus ? llvm::errs() : VS) << " Match macro "
VS << " Match macro "
<< ExpansionRoot->Name
<< " with "
<< ExpansionRoot->SubtreeNodes.size()
<< " (nested) expansions\n";
//ExpansionRoot->SpellingRange.dump(SM);
//ExpansionLoc.dump(SM);
if (m_settings.verbose || focus)
if (m_settings.verbose)
ST->dumpColor();
for (auto Expansion : ExpansionRoot->SubtreeNodes) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment