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