summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-modernize/Core/CustomMatchers.h
blob: 485a47cd41762d3a8df225fdad65a14bbf8a514a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//===-- Core/CustomMatchers.h - Perf measurement helpers -----------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief This file provides custom matchers to be used by different
/// transforms that requier the same matchers.
///
//===----------------------------------------------------------------------===//

#ifndef CLANG_MODERNIZE_CUSTOMMATCHERS_H
#define CLANG_MODERNIZE_CUSTOMMATCHERS_H

#include "clang/ASTMatchers/ASTMatchers.h"

namespace clang {
namespace ast_matchers {

/// \brief Matches declarations whose declaration context is the C++ standard
/// library namespace \c std.
///
/// Note that inline namespaces are silently ignored during the lookup since
/// both libstdc++ and libc++ are known to use them for versioning purposes.
///
/// Given
/// \code
///   namespace ns {
///     struct my_type {};
///     using namespace std;
///   }
///
///   using std::vector;
///   using ns::my_type;
///   using ns::list;
/// \endcode
/// usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(isFromStdNamespace())))
///   matches "using std::vector" and "using ns::list".
AST_MATCHER(Decl, isFromStdNamespace) {
  const DeclContext *D = Node.getDeclContext();

  while (D->isInlineNamespace())
    D = D->getParent();

  if (!D->isNamespace() || !D->getParent()->isTranslationUnit())
    return false;

  const IdentifierInfo *Info = cast<NamespaceDecl>(D)->getIdentifier();

  return Info && Info->isStr("std");
}
} // namespace ast_matchers
} // namespace clang

#endif // CLANG_MODERNIZE_CUSTOMMATCHERS_H
OpenPOWER on IntegriCloud