Skip to content
This repository has been archived by the owner on Oct 13, 2021. It is now read-only.

Commit

Permalink
Merge better naming of anonymous namespaces. Close #643.
Browse files Browse the repository at this point in the history
  • Loading branch information
erikrose committed Feb 28, 2017
2 parents 6081322 + 40673a0 commit 2dcad1d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
19 changes: 17 additions & 2 deletions dxr/plugins/clang/dxr-index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,22 @@ class IndexConsumer : public ASTConsumer,
recordValue("locend", locationToString(afterToken(endLoc)));
}

// Given a declaration, get the name of the file containing the corresponding
// definition or the name of the file containing the declaration if no
// definition can be found.
std::string getRealFilenameForDefinition(const NamedDecl &d) {
const NamedDecl *decl = &d;

if (const FunctionDecl *fd = dyn_cast<FunctionDecl>(decl)) {
const FunctionDecl *def = 0;
if (fd->isDefined(def))
decl = def;
}

const std::string &filename = sm.getFilename(decl->getLocation());
return getFileInfo(filename)->realname;
}

// This is a wrapper around NamedDecl::getQualifiedNameAsString.
// It produces more qualified output to distinguish several cases
// which would otherwise be ambiguous.
Expand Down Expand Up @@ -376,8 +392,7 @@ class IndexConsumer : public ASTConsumer,
const std::string anon_ns = "<anonymous namespace>";
#endif
if (StringRef(ret).startswith(anon_ns)) {
const std::string &filename = ci.getFrontendOpts().Inputs[0].getFile().str();
const std::string &realname = getFileInfo(filename)->realname;
const std::string &realname = getRealFilenameForDefinition(d);
ret = "(" + ret.substr(1, anon_ns.size() - 2) + " in " + realname + ")" +
ret.substr(anon_ns.size());
}
Expand Down
2 changes: 2 additions & 0 deletions dxr/plugins/clang/tests/test_anon_ns/code/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "main2.h"
#include "main3.h"

namespace
{
Expand All @@ -11,5 +12,6 @@ int main()
{
foo(); /* calling foo in main */
bar();
baz();
return 0;
}
2 changes: 2 additions & 0 deletions dxr/plugins/clang/tests/test_anon_ns/code/main2.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "main2.h"
#include "main3.h"

namespace
{
Expand All @@ -10,4 +11,5 @@ namespace
void bar()
{
foo(); /* calling foo in main2 */
baz();
}
6 changes: 6 additions & 0 deletions dxr/plugins/clang/tests/test_anon_ns/code/main3.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace
{
void baz() /* in main3.h */
{
}
}
16 changes: 12 additions & 4 deletions dxr/plugins/clang/tests/test_anon_ns/test_anon_ns.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ class AnonymousNamespaceTests(DxrInstanceTestCase):

def test_function(self):
self.found_line_eq('+function:"(anonymous namespace in main.cpp)::foo()"',
'void <b>foo</b>() /* in main */', 5)
'void <b>foo</b>() /* in main */', 6)
self.found_line_eq('+function:"(anonymous namespace in main2.cpp)::foo()"',
'void <b>foo</b>() /* in main2 */', 5)
'void <b>foo</b>() /* in main2 */', 6)

def test_function_ref(self):
self.found_line_eq('+function-ref:"(anonymous namespace in main.cpp)::foo()"',
'<b>foo</b>(); /* calling foo in main */', 12)
'<b>foo</b>(); /* calling foo in main */', 13)
self.found_line_eq('+function-ref:"(anonymous namespace in main2.cpp)::foo()"',
'<b>foo</b>(); /* calling foo in main2 */', 12)
'<b>foo</b>(); /* calling foo in main2 */', 13)

def test_anonymous_namespace_in_header(self):
self.found_line_eq('+function:"(anonymous namespace in main3.h)::baz()"',
'void <b>baz</b>() /* in main3.h */', 3)
self.found_files_eq('+function-ref:"(anonymous namespace in main3.h)::baz()"', [
"main.cpp",
"main2.cpp"])

0 comments on commit 2dcad1d

Please sign in to comment.