1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
5 * Based on LLVM/Clang.
6 *
7 * This file is distributed under the University of Illinois Open Source
8 * License. See LICENSE.TXT for details.
9 *
10 */
11 #ifndef LO_CLANG_SHARED_PLUGINS
12
13 #include "plugin.hxx"
14 #include "check.hxx"
15 #include "compat.hxx"
16
17 #include <clang/Lex/Lexer.h>
18
19 #include <fstream>
20 #include <set>
21
22 namespace
23 {
24
25 class SalLogAreas
26 : public loplugin::FilteringPlugin< SalLogAreas >
27 {
28 public:
SalLogAreas(const loplugin::InstantiationData & data)29 explicit SalLogAreas( const loplugin::InstantiationData& data )
30 : FilteringPlugin(data), inFunction(nullptr) {}
31
preRun()32 bool preRun() override {
33 return true;
34 }
35
run()36 void run() override {
37 if (preRun())
38 {
39 lastSalDetailLogStreamMacro = SourceLocation();
40 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
41 }
42 }
43
44 bool VisitFunctionDecl( const FunctionDecl* function );
45 bool VisitCallExpr( const CallExpr* call );
46 private:
47 void checkArea( StringRef area, SourceLocation location );
48 void checkAreaSyntax(StringRef area, SourceLocation location);
49 void readLogAreas();
50 const FunctionDecl* inFunction;
51 SourceLocation lastSalDetailLogStreamMacro;
52 std::set< std::string > logAreas;
53 #if 0
54 std::string firstSeenLogArea;
55 SourceLocation firstSeenLocation;
56 #endif
57 };
58
59 /*
60 This is a compile check.
61
62 Check area used in SAL_INFO/SAL_WARN macros against the list in include/sal/log-areas.dox and
63 report if the area is not listed there. The fix is either use a proper area or add it to the list
64 if appropriate.
65 */
66
VisitFunctionDecl(const FunctionDecl * function)67 bool SalLogAreas::VisitFunctionDecl( const FunctionDecl* function )
68 {
69 inFunction = function;
70 return true;
71 }
72
VisitCallExpr(const CallExpr * call)73 bool SalLogAreas::VisitCallExpr( const CallExpr* call )
74 {
75 if( ignoreLocation( call ))
76 return true;
77 const FunctionDecl* func = call->getDirectCallee();
78 if( !func || !func->getIdentifier())
79 return true;
80
81 auto name = func->getName();
82 if( !( name == "sal_detail_log" || name == "log" || name == "DbgUnhandledException" || name == "XMLOFF_WARN_UNKNOWN") )
83 return true;
84
85 auto tc = loplugin::DeclCheck(func);
86 enum class LogCallKind { Sal, DbgUnhandledException};
87 LogCallKind kind;
88 int areaArgIndex;
89 if( tc.Function("XMLOFF_WARN_UNKNOWN") )
90 {
91 kind = LogCallKind::Sal; // fine
92 areaArgIndex = 0;
93 }
94 else if( tc.Function("sal_detail_log") || tc.Function("log").Namespace("detail").Namespace("sal").GlobalNamespace() )
95 {
96 kind = LogCallKind::Sal; // fine
97 areaArgIndex = 1;
98 }
99 else if( tc.Function("DbgUnhandledException").GlobalNamespace() )
100 {
101 kind = LogCallKind::DbgUnhandledException; // ok
102 areaArgIndex = 3;
103 }
104 else
105 return true;
106
107 // The SAL_DETAIL_LOG_STREAM macro expands to two calls to sal::detail::log(),
108 // so do not warn repeatedly about the same macro (the area->getLocStart() of all the calls
109 // from the same macro should be the same).
110 if( kind == LogCallKind::Sal )
111 {
112 SourceLocation expansionLocation = compiler.getSourceManager().getExpansionLoc( compat::getBeginLoc(call));
113 if( expansionLocation == lastSalDetailLogStreamMacro )
114 return true;
115 lastSalDetailLogStreamMacro = expansionLocation;
116 };
117 if( const clang::StringLiteral* area = dyn_cast< clang::StringLiteral >( call->getArg( areaArgIndex )->IgnoreParenImpCasts()))
118 {
119 if( area->getKind() == clang::StringLiteral::Ascii )
120 checkArea( area->getBytes(), area->getExprLoc());
121 else
122 report( DiagnosticsEngine::Warning, "unsupported string literal kind (plugin needs fixing?)",
123 compat::getBeginLoc(area));
124 return true;
125 }
126 if( loplugin::DeclCheck(inFunction).Function("log").Namespace("detail").Namespace("sal").GlobalNamespace()
127 || loplugin::DeclCheck(inFunction).Function("sal_detail_logFormat").GlobalNamespace() )
128 return true; // These functions only forward to sal_detail_log, so ok.
129 if( loplugin::DeclCheck(inFunction).Function("XMLOFF_WARN_UNKNOWN").GlobalNamespace())
130 return true;
131 if( call->getArg( areaArgIndex )->isNullPointerConstant( compiler.getASTContext(),
132 Expr::NPC_ValueDependentIsNotNull ) != Expr::NPCK_NotNull )
133 { // If the area argument is a null pointer, that is allowed only for SAL_DEBUG.
134 const SourceManager& source = compiler.getSourceManager();
135 for( SourceLocation loc = compat::getBeginLoc(call);
136 loc.isMacroID();
137 loc = compat::getImmediateExpansionRange(source, loc ).first )
138 {
139 StringRef inMacro = Lexer::getImmediateMacroName( loc, source, compiler.getLangOpts());
140 if( inMacro == "SAL_DEBUG" || inMacro == "SAL_DEBUG_BACKTRACE" )
141 return true; // ok
142 }
143 report( DiagnosticsEngine::Warning, "missing log area",
144 compat::getBeginLoc(call->getArg( 1 )->IgnoreParenImpCasts()));
145 return true;
146 }
147 report( DiagnosticsEngine::Warning, "cannot analyse log area argument (plugin needs fixing?)",
148 compat::getBeginLoc(call));
149 return true;
150 }
151
checkArea(StringRef area,SourceLocation location)152 void SalLogAreas::checkArea( StringRef area, SourceLocation location )
153 {
154 if( logAreas.empty())
155 readLogAreas();
156 if( !logAreas.count( area.str() ))
157 {
158 report( DiagnosticsEngine::Warning, "unknown log area '%0' (check or extend include/sal/log-areas.dox)",
159 location ) << area;
160 checkAreaSyntax(area, location);
161 return;
162 }
163 // don't leave this alive by default, generates too many false+
164 #if 0
165 if (compiler.getSourceManager().isInMainFile(location))
166 {
167 auto matchpair = [this,area](StringRef p1, StringRef p2) {
168 return (area == p1 && firstSeenLogArea == p2) || (area == p2 && firstSeenLogArea == p1);
169 };
170 // these are "cross-module" log areas
171 if (area == "i18n" || area == "lok" || area == "lok.tiledrendering")
172 ;
173 // these appear to be cross-file log areas
174 else if ( area == "chart2"
175 || area == "oox.cscode" || area == "oox.csdata"
176 || area == "slideshow.verbose"
177 || area == "sc.opencl"
178 || area == "sc.core.formulagroup"
179 || area == "sw.pageframe" || area == "sw.idle" || area == "sw.level2"
180 || area == "sw.docappend" || area == "sw.mailmerge"
181 || area == "sw.uno"
182 || area == "vcl.layout" || area == "vcl.a11y"
183 || area == "vcl.gdi.fontmetric" || area == "vcl.opengl"
184 || area == "vcl.harfbuzz" || area == "vcl.eventtesting"
185 || area == "vcl.schedule" || area == "vcl.unity"
186 || area == "xmlsecurity.comp"
187 )
188 ;
189 else if (firstSeenLogArea == "")
190 {
191 firstSeenLogArea = area;
192 firstSeenLocation = location;
193 }
194 // some modules do this deliberately
195 else if (firstSeenLogArea.compare(0, 3, "jfw") == 0
196 || firstSeenLogArea.compare(0, 6, "opencl") == 0)
197 ;
198 // mixing these in the same file seems legitimate
199 else if (
200 matchpair("chart2.pie.label.bestfit", "chart2.pie.label.bestfit.inside")
201 || matchpair("editeng", "editeng.chaining")
202 || matchpair("oox.drawingml", "oox.cscode")
203 || matchpair("oox.drawingml", "oox.drawingml.gradient")
204 || matchpair("sc.core", "sc.core.grouparealistener")
205 || matchpair("sc.orcus", "sc.orcus.condformat")
206 || matchpair("sc.orcus", "sc.orcus.style")
207 || matchpair("sc.orcus", "sc.orcus.autofilter")
208 || matchpair("svx", "svx.chaining")
209 || matchpair("sw.ww8", "sw.ww8.level2")
210 || matchpair("writerfilter", "writerfilter.profile")
211 )
212 ;
213 else if (firstSeenLogArea != area)
214 {
215 report( DiagnosticsEngine::Warning, "two different log areas '%0' and '%1' in the same file?",
216 location ) << firstSeenLogArea << area;
217 report( DiagnosticsEngine::Note, "first area was seen here",
218 firstSeenLocation );
219 }
220 }
221 #endif
222 }
223
checkAreaSyntax(StringRef area,SourceLocation location)224 void SalLogAreas::checkAreaSyntax(StringRef area, SourceLocation location) {
225 for (std::size_t i = 0;;) {
226 std::size_t j = area.find('.', i);
227 if (j == StringRef::npos) {
228 j = area.size();
229 }
230 if (j == i) {
231 goto bad;
232 }
233 for (; i != j; ++i) {
234 auto c = area[i];
235 if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z'))) {
236 goto bad;
237 }
238 }
239 if (j == area.size()) {
240 return;
241 }
242 i = j + 1;
243 }
244 bad:
245 report(
246 DiagnosticsEngine::Warning,
247 "invalid log area syntax '%0'%1 (see include/sal/log.hxx for details)",
248 location)
249 << area << (location.isValid() ? "" : " in include/sal/log-areas.dox");
250 }
251
readLogAreas()252 void SalLogAreas::readLogAreas()
253 {
254 std::ifstream is( SRCDIR "/include/sal/log-areas.dox" );
255 while( is.good())
256 {
257 std::string line;
258 getline( is, line );
259 size_t pos = line.find( "@li @c " );
260 if( pos != std::string::npos )
261 {
262 pos += strlen( "@li @c " );
263 size_t end = line.find( ' ', pos );
264 std::string area;
265 if( end == std::string::npos )
266 area = line.substr( pos );
267 else if( pos != end )
268 area = line.substr( pos, end - pos );
269 checkAreaSyntax(area, SourceLocation());
270 logAreas.insert(area);
271 }
272 }
273 // If you get this error message, you possibly have too old icecream (ICECC_EXTRAFILES is needed).
274 if( logAreas.empty())
275 report( DiagnosticsEngine::Warning, "error reading log areas" );
276 }
277
278 static loplugin::Plugin::Registration< SalLogAreas > sallogareas( "sallogareas" );
279
280 } // namespace
281
282 #endif // LO_CLANG_SHARED_PLUGINS
283
284 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
285