clang-tools 20.0.0git
DesignatedInitializers.h
Go to the documentation of this file.
1//===--- DesignatedInitializers.h - clang-tidy ------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file provides utilities for designated initializers.
11///
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Expr.h"
15#include "clang/Basic/SourceLocation.h"
16#include "llvm/ADT/DenseMap.h"
17
18namespace clang::tidy::utils {
19
20/// Get designators describing the elements of a (syntactic) init list.
21///
22/// Given for example the type
23/// \code
24/// struct S { int i, j; };
25/// \endcode
26/// and the definition
27/// \code
28/// S s{1, 2};
29/// \endcode
30/// calling `getUnwrittenDesignators` for the initializer list expression
31/// `{1, 2}` would produce the map `{loc(1): ".i", loc(2): ".j"}`.
32///
33/// It does not produce designators for any explicitly-written nested lists,
34/// e.g. `{1, .j=2}` would only return `{loc(1): ".i"}`.
35///
36/// It also considers structs with fields of record types like
37/// `struct T { S s; };`. In this case, there would be designators of the
38/// form `.s.i` and `.s.j` in the returned map.
39llvm::DenseMap<clang::SourceLocation, std::string>
40getUnwrittenDesignators(const clang::InitListExpr *Syn);
41
42} // namespace clang::tidy::utils
llvm::DenseMap< SourceLocation, std::string > getUnwrittenDesignators(const InitListExpr *Syn)